os.walk walks too much

Marcello Pietrobon teiffel at attglobal.net
Wed Feb 25 07:39:34 EST 2004


Hello,
I am using Pyton 2.3
I desire to walk a directory without recursion

this only partly works:
def walk_files() :
    for root, dirs, files in os.walk(top, topdown=True):
        for filename in files:
            print( "file:" + os.path.join(root, filename) )
        for dirname in dirs:
             dirs.remove( dirname )
because it skips all the subdirectories but one.

this *does not* work at all
def walk_files() :
    for root, dirs, files in os.walk(top, topdown=True):
        for filename in files:
            print( "file:" + os.path.join(root, filename) )
        dirs = []

This is surprizing to me.
Is this a glitch ?

How should I implement this ?
Maybe it would be good to put it in the os. walk documentation ?

Cheers,
Marcello






More information about the Python-list mailing list