stymied with os.walk - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

stymied with os.walk

PharazonPharazon Posts: 3Member
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 "". 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!

Comments

  • DrostDrost Posts: 24Member
    : 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 "". 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:
    [code]
    /tmp
    /alfa
    one.txt
    two.txt
    /beta
    /gamma
    three.txt
    four.txt
    five.txt
    [/code]

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

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

    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
  • PharazonPharazon Posts: 3Member
    : : 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 "". 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:
    : [code]
    : /tmp
    : /alfa
    : one.txt
    : two.txt
    : /beta
    : /gamma
    : three.txt
    : four.txt
    : five.txt
    : [/code]
    :
    : With this code:
    : [code]
    : import os
    : dir_list = os.walk('/tmp')
    : for i in dir_list:
    : print i
    : [/code]
    :
    : You'll recieve:
    : [code]
    : ('/tmp', ['alfa', 'beta'], ['five.txt'])
    : ('/tmp/alfa', [], ['one.txt', 'two.txt'])
    : ('/tmp/beta', ['gamma'], ['four.txt'])
    : ('/tmp/beta/gamma', [], ['three.txt'])
    : [/code]
    :
    : 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.
  • infidelinfidel Posts: 2,900Member
    : 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:

    [code]
    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)
    [/code]

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


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

Sign In or Register to comment.