1
votes

I'm developing my first "Hello World" on Python! I used to be Visual Basic programmer a long long time ago...

I'm trying to use Namedtuple Collections like below:

#!/usr/bin/env python3
    
import collections
    
#Tipo da colecao
File = collections.namedtuple('File',['abspath' ,'basename','filextention','filetype'])
Files = []
    
Files.append(File('/home/user/teste1.txt', 'teste1.txt','txt',''))
Files.append(File('/home/user/teste2.txt', 'teste2.txt','txt',''))
Files.append(File('/home/user/teste3.txt', 'teste2.txt','txt',''))
    
for lCtd in range(0,len(Files)):
    print(Files[lCtd].abspath,Files[lCtd].basename,Files[lCtd].filextention)
    
for lCtd in range(0,len(Files)):
    #>>>>>How can I do this?!<<<<< 
    Files[lCtd].filetype = 'A' 

If have a best way to do this(or the all code), I'm all ears...

Thanks a lot!

2
namedtuples are immutable. You could try using a list of dicts instead, or recordtype. - PGHE
Great! How can this works? Could you show with the code (using the same logic that I'm trying above)? I started in Python this weekend... sorry - Fperuso
Maybe it's different in VB, but in Python, it's much discouraged to have variable names start with uppercase characters (file, not File, files, not Files). Uppercase first characters signal a type vs a variable. - CryptoFool
Thanks @Steve! I have a lot of thing to learn in Python... - Fperuso

2 Answers

1
votes

Named tuples are useful, but they are immutable: their attributes cannot be changed after they're created. If you want a similarly convenient data-holder that's mutable, you might be interested in data classes, as long as you're using Python 3.7 or newer.

from dataclasses import dataclass

@dataclass
class File:
    file_name: str
    abs_path: str
    base_name: str
    file_extension: str
    # The '=' below sets a default value. (You could use '', but None is more
    # commonly used for this.)
    file_type: str =  None

files = [
    File('/home/user/test1.txt', 'test1.txt', 'txt'),
    File('/home/user/test2.txt', 'test2.txt', 'txt'),
    File('/home/user/test3.txt', 'test3.txt', 'txt'),
]
    
for file in files:
    print(file.abs_path, file.base_name, file.file_extension)
    # This will update the file_type attribute for each file:
    file.file_type = 'A'

Note that the more Pythonic way to loop through a list (or any sequence) is to not rely on len and an index, but simply to loop through the items themselves.

All this said, if you're looking for a simple way to work with files, such as getting their names and extensions, you should probably also look at Pathlib.

from pathlib import Path

file = Path('/home/user/test1.txt')
print(str(file), file.name, file.suffix)

# Output: /home/user/test1.txt test1.txt .txt

P.S. file was a builtin in Python 2, and many editors may still highlight it as such, but it should be fine to use as a variable/class name in Python 3.

0
votes

It seems that what you really want here is to define your own class, like this:

class File:
    # make a constructor, with a default parameter (for convenience!)
    def __init__(self, abspath, basename, filextention, filetype = ''):
        self.abspath = abspath
        self.basename = basename
        self.filextention = filextention
        self.filetype = filetype

Files = []

# this part is almost the same, but you don't need the last parameter
# because we gave it a default value    
Files.append(File('/home/user/teste1.txt', 'teste1.txt','txt'))
Files.append(File('/home/user/teste2.txt', 'teste2.txt','txt'))
Files.append(File('/home/user/teste3.txt', 'teste2.txt','txt'))

# this is a better way to loop over a list in Python:
for curFile in Files:
    print(curFile.abspath, curFile.basename, curFile.filextention)

for curFile in Files:
    curFile.filextention = 'A' # in Python all fields are public by default and can be assigned like this