LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-29-2013, 05:53 PM   #1
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,171

Rep: Reputation: 60
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.
 
Old 10-30-2013, 04:39 AM   #2
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 493

Rep: Reputation: 211Reputation: 211Reputation: 211
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
 
Old 10-30-2013, 05:49 AM   #3
pgpython
Member
 
Registered: Dec 2005
Location: Sheffield, UK
Distribution: Gentoo
Posts: 142

Rep: Reputation: 32
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
 
1 members found this post helpful.
Old 10-30-2013, 07:21 AM   #4
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 493

Rep: Reputation: 211Reputation: 211Reputation: 211
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).

Last edited by mina86; 10-30-2013 at 07:23 AM.
 
1 members found this post helpful.
Old 10-31-2013, 11:56 AM   #5
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,171

Original Poster
Rep: Reputation: 60
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

Last edited by metallica1973; 10-31-2013 at 12:21 PM.
 
Old 10-31-2013, 01:25 PM   #6
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 493

Rep: Reputation: 211Reputation: 211Reputation: 211
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()”.
 
1 members found this post helpful.
Old 10-31-2013, 01:57 PM   #7
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,171

Original Poster
Rep: Reputation: 60
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...
?

Last edited by metallica1973; 10-31-2013 at 04:38 PM.
 
Old 10-31-2013, 04:51 PM   #8
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 493

Rep: Reputation: 211Reputation: 211Reputation: 211
“content[2][0]” will access those individual files, but as I described, it is not how you want your function to work.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Python - Sorting Lists inside of Lists LXer Syndicated Linux News 0 06-06-2013 02:00 PM
[SOLVED] python def question gary_in_springhill Programming 7 02-02-2010 08:13 PM
Error while populating data from text files to a 2d linked lists PMR Linux - Newbie 3 05-17-2009 07:42 PM
Python and nested lists problem tadeas Programming 2 03-22-2009 06:16 AM
LXer: Unique Sorting Of Lists And Lists Of Lists With Perl For Linux Or Unix LXer Syndicated Linux News 0 09-05-2008 02:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration