Python

Moderators: None (Apply to moderate this forum)
Number of threads: 474
Number of posts: 1166

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

Report
stymied with os.walk Posted by Pharazon on 19 May 2005 at 6:45 PM
I am developing a program which requires the capability to go through all the files, etc in a set user(determined at user's login, usually their own) and send the results to other modules. I am using Python 2.3 as 2.4 has strange problems with modules like pygame, etc.

Anyway, I found os.walk() and I tried using it, but all I get is a "<generator function at (some hex address)>". I even tried using the examples given in the default help file, but it doesn't work. I was wondering how to properly use os.walk, but any further info would be greatly appreciated.

BTW, the help with the SHA module was invaluable, so thanx to all that helped!
Report
Re: stymied with os.walk Posted by Drost on 20 May 2005 at 9:09 AM
: I am developing a program which requires the capability to go through all the files, etc in a set user(determined at user's login, usually their own) and send the results to other modules. I am using Python 2.3 as 2.4 has strange problems with modules like pygame, etc.
:
: Anyway, I found os.walk() and I tried using it, but all I get is a "<generator function at (some hex address)>". I even tried using the examples given in the default help file, but it doesn't work. I was wondering how to properly use os.walk, but any further info would be greatly appreciated.
:
: BTW, the help with the SHA module was invaluable, so thanx to all that helped!
:

Suppose the /tmp directory contains:
/tmp
    /alfa
        one.txt
        two.txt
    /beta
        /gamma
            three.txt
        four.txt
    five.txt


With this code:
import os
dir_list = os.walk('/tmp')
for i in dir_list:
  print i


You'll recieve:
('/tmp', ['alfa', 'beta'], ['five.txt'])
('/tmp/alfa', [], ['one.txt', 'two.txt'])
('/tmp/beta', ['gamma'], ['four.txt'])
('/tmp/beta/gamma', [], ['three.txt'])


That's a tuple on each run containing the actual path (as a string), the subdirectories (in a list) and the files (in a list) on that level in the filesystem...

os.walk()'s other parameters are well described in the manual.

Hope this helps

Drost
Report
Re: stymied with os.walk Posted by Pharazon on 20 May 2005 at 5:17 PM
: : I am developing a program which requires the capability to go through all the files, etc in a set user(determined at user's login, usually their own) and send the results to other modules. I am using Python 2.3 as 2.4 has strange problems with modules like pygame, etc.
: :
: : Anyway, I found os.walk() and I tried using it, but all I get is a "<generator function at (some hex address)>". I even tried using the examples given in the default help file, but it doesn't work. I was wondering how to properly use os.walk, but any further info would be greatly appreciated.
: :
: : BTW, the help with the SHA module was invaluable, so thanx to all that helped!
: :
:
: Suppose the /tmp directory contains:
:
: /tmp
:     /alfa
:         one.txt
:         two.txt
:     /beta
:         /gamma
:             three.txt
:         four.txt
:     five.txt
: 

:
: With this code:
:
: import os
: dir_list = os.walk('/tmp')
: for i in dir_list:
:   print i
: 

:
: You'll recieve:
:
: ('/tmp', ['alfa', 'beta'], ['five.txt'])
: ('/tmp/alfa', [], ['one.txt', 'two.txt'])
: ('/tmp/beta', ['gamma'], ['four.txt'])
: ('/tmp/beta/gamma', [], ['three.txt'])
: 

:
: That's a tuple on each run containing the actual path (as a string), the subdirectories (in a list) and the files (in a list) on that level in the filesystem...
:
: os.walk()'s other parameters are well described in the manual.
:
: Hope this helps
:
: Drost
:
Don't worry. I just looked up generators and found the .next() command associated with them. That allowed me to use a recursive function to crawl through ALL files. But thank you for helping.
Report
Re: stymied with os.walk Posted by infidel on 23 May 2005 at 7:55 AM
: Don't worry. I just looked up generators and found the .next() command associated with them. That allowed me to use a recursive function to crawl through ALL files. But thank you for helping.

You shouldn't normally need to write your own recursive function to call a generator's next() method. os.walk is already recursive. Generators are specifically designed to be used in "for" loops. I use os.walk all the time like this:

root = "C:\\Development"
for dir, subdirs, files in os.walk(root):
    for f in files:
        if f[-4:].lower() == '.vbp':
            print os.path.join(dir, f)


This will find every visual basic project file anywhere under my C:\Development folder.


infidel

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





 

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.