LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Populating Lists in Def using Python (https://www.linuxquestions.org/questions/programming-9/populating-lists-in-def-using-python-4175482688/)

metallica1973 10-29-2013 04:53 PM

Populating Lists in Def using Python
 
Dipping around in python again and need to create a def that will populate a list(content) with the files that os.walk finds from within this directory and then I will re.search through each files looking for content. In learning Python, can someone point me in the right direction. This is what I have so far.

Code:

def list_files ( content=None ):
    if content is None:
        content = []
        for files in os.walk('var/www/html/data/customer/log', topdown=True):
            content.append(files)
            return content

Many thanks in advanced.

mina86 10-30-2013 03:39 AM

Why is the function taking an argument? If you want to have the list remembered the second time function is called, use global variable. Put the return outside of for and it will work. Most importantly, does your function does anything other then just pass through whatever os.walk returns?

Code:

_LIST_FILES = [None]

def list_files():
    if _LIST_FILES[0] is None:
        _LIST_FILES[:] = list(os.walk('var/www/html/data/customer/log', topdown=True))
    return _LIST_FILES


pgpython 10-30-2013 04:49 AM

what you have is basically ok. Don't use globals unless you have to, (doing so will make your code unmaintable later). I believe you want extend not append and you have the return in the wrong place

Code:

for files in os.walk('var/www/html/data/customer/log', topdown=True):
    content.extend(files)
return content


mina86 10-30-2013 06:21 AM

Ah, right, I forgot that os.walk has a weird return type. What you want is:
Code:

for dir, _, files in os.walk('var/www/html/data/customer/log'):
    content.extend(os.path.join(dir, file) for file in files)
return content

(Also note that topdown=True is the default).

metallica1973 10-31-2013 10:56 AM

ok I got my function to work using:

Code:

def list_files():
    content = []
    for files in os.walk('var/www/html/data/customer/log'):
      content.extend(files)
    return content

next stupid question is how do I access one of the many files in the function? So the results of os.walk populates my list -- content [] with the files in the directory:

Code:

list_files()
print list_files()
['var/www/html/data/customer/log', [], ['file1', 'file2', 'file3', 'file4']]

so how would I access that information so I can add additional logic to my script? when I attempt to access anything in my list, I get the following error

Code:

content[3]

NameError: name 'content' is not defined

in other words, how can I access:

Code:

['file1', 'file2', 'file3', 'file4']]
when not using a function I can access the elements a such:

Code:

In [66]: func = []

In [67]: func.extend( ("blah1","blah2" ) )

In [68]: print func
['blah1', 'blah2']
In [69]:func[0]
blah1

In [70]:func[1]
blah2


mina86 10-31-2013 12:25 PM

First of all, if you do not care about traversing the directory hierarchy recursively, instead of using os.walk, just use os.listdir, as in os.listdir('var/www/html/data/customer/log'). Depending on what you are doing, you may need to filter directories out of the returned list.

If you do care about traversing the structure recursively, then os.walk is what you want, but you need to keep in mind that it yields a (dir, dirs, files) tuples and what you really want is a sequence of file names from files prefixed with dir. This is what my code in #4 is doing.

As for “NameError: name 'content' is not defined”, the variable is local to the function, so it cannot be accessed outside of it. What you want is to (a) just get rid of the function and run the code in outer context, or (b) capture return of the function as in “content = list_files()”.

metallica1973 10-31-2013 12:57 PM

Many thank you's

I will use option (a) but in better understanding choice (b) content = list_files(), I noticed that accessing the elements in:

Code:

content[(0)]
Out[92]: 'var/www/html/data/customer/log'
content[(2)]
Out[92]: '[file1',
'file2',
'file3',
'file4']

you cannot access individual elements,(Forgive me for my stupid questions,learning Python), like:
Code:

content[(1)]
file1
content[(2)]
file2
content[(3)]
file4
etc...

?

mina86 10-31-2013 03:51 PM

“content[2][0]” will access those individual files, but as I described, it is not how you want your function to work.


All times are GMT -5. The time now is 12:35 PM.