ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
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
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:
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 11:21 AM.
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()”.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.