Populating Lists in Def using Python | Unix Linux Forums | Programming

  Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Populating Lists in Def using Python

Programming


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-29-2013
metallica1973 metallica1973 is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 24 September 2014, 10:05 AM EDT
Location: Washington D.C
Posts: 176
Thanks: 17
Thanked 2 Times in 2 Posts
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.
Sponsored Links
    #2  
Old 10-29-2013
targzeta's Avatar
targzeta targzeta is offline
Registered User
 
Join Date: Oct 2013
Last Activity: 3 December 2013, 1:49 PM EST
Location: Pisa, Italy
Posts: 27
Thanks: 1
Thanked 5 Times in 5 Posts
Basically os.walk returns a tuple (dirpath, dirnames, filenames). So, if you want the full path of every files, you have to join dirpath and filename:
Code:
content = []
for root, dirs, files in os.walk("/tmp", topdown=False):
    for name in files:
        content.append(os.path.join(root, name))

Emanuele

Note: if your directory doesn't have subdirs, you can also use os.listdir.
Sponsored Links
    #3  
Old 10-31-2013
metallica1973 metallica1973 is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 24 September 2014, 10:05 AM EDT
Location: Washington D.C
Posts: 176
Thanks: 17
Thanked 2 Times in 2 Posts
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

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]: print func[0]
blah1

In [70]: print func[1]
blah2


Last edited by metallica1973; 10-31-2013 at 12:19 PM..
    #4  
Old 11-05-2013
dinaafifi dinaafifi is offline
Registered User
 
Join Date: Nov 2013
Last Activity: 23 November 2013, 4:13 PM EST
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
thnx for help
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Separate Text File into Two Lists Using Python Tyler_92 Shell Programming and Scripting 0 03-03-2013 12:30 AM
[Python] Compare 2 lists dagendy Homework & Coursework Questions 1 12-15-2012 07:58 AM
Python: Compare 2 word lists Bloomy Programming 6 06-24-2011 10:24 PM
Shell Script to Create non-duplicate lists from two lists mlv_99 Shell Programming and Scripting 7 04-06-2010 08:59 PM



All times are GMT -4. The time now is 09:35 AM.