Python

Moderators: None (Apply to moderate this forum)
Number of threads: 473
Number of posts: 1172

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
I hope this'll get answered Posted by Gregry2 on 25 Jan 2006 at 3:25 AM
I just want to make a simple script that can work with files. OS's walk() seems to be able to single out all the directories in the current working directory according to the docs...

grr...but it doesn't return a tuple as it says, it only gives a `generator' object, one I called help on, but had no luck whatsoever

I hope this gets answered, please help me, at least to single out all the directories in a directory.

thanx
{2}rIng
Report
Re: I hope this'll get answered Posted by infidel on 25 Jan 2006 at 8:31 AM
: I just want to make a simple script that can work with files. OS's walk() seems to be able to single out all the directories in the current working directory according to the docs...
:
: grr...but it doesn't return a tuple as it says, it only gives a `generator' object, one I called help on, but had no luck whatsoever
:
: I hope this gets answered, please help me, at least to single out all the directories in a directory.

What is it that you want returned? os.walk() returns a generator, and generators are special objects that only yield the next value of a sequence when you call their next() method. Think of it as iterating over a list where the items in the list only exist for that iteration.

Each call to the next() method of the os.walk generator reurns a tuple containing a directory, a list of subdirectories in that directory, and a list of files in that directory. It recurses through all subdirectories under the path you pass to os.walk(), unless you remove them from the subdirs list at runtime.

So here's how you use os.walk():

>>> import os
>>> root = 'C:\\'
>>> for dir, subdirs, files in os.walk(root):
... 	for f in files:
... 		print os.path.join(dir, f)


The reason for generators is so that you don't have to wait for an entire sequence to be created before using it, and you don't have to use up memory to hold the whole thing in the meantime. It's really very cool.

Let's say you want to find every python script on your drive and do something with the path. You can write your own generator to wrap os.walk and "filter" the results a bit:

import os

def walk_py(top):
    for dir, subdirs, files in os.walk(top):
        for f in files:
            if f[-3:].lower() == '.py':
                yield dir, f

def main():
    root = 'K:\\src\\py'
    for dir, py in walk_py(root):
        print os.path.join(dir, py)

if __name__ == '__main__': main()


Any function containing a "yield" statement is compiled by python to return a generator object.


infidel

$ select * from users where clue > 0
no rows returned


Report
Re: I hope this'll get answered Posted by Gregry2 on 26 Jan 2006 at 3:03 AM
I'm an idiot! I remember this thing from the python tutorial!...it didn't stick...generator seemed to ring a bell, I just forgot about it, guh...

thanx, I can do this now!
{2}rIng



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.