os.walk walks too much

Marcello Pietrobon teiffel at attglobal.net
Wed Feb 25 14:31:32 EST 2004


Thank you everybody for all the answers.
They all have been useful :)

I have only two question reguarding Peter Otten's answer

1)
What is the difference between

 for dirname in dirs:
    dirs.remove( dirname )

and

 for dirname in dirs[:]:
    dirs.remove( dirname )

( I understand and agree that there are better ways, and at list a reverse iterator should be used )



2)

def walk_files(root, recursive=False):
    for path, dirs, files in os.walk(root):
        for fn in files:
            yield os.path.join(path, fn)
            if not recursive:
                break

seems not correct to me:

because I tend to assimilate yield to a very special return statement
so I think the following is correct

def walk_files(root, recursive=False):
    for path, dirs, files in os.walk(root):
        for fn in files:
            yield os.path.join(path, fn)
        if not recursive:
            break


is that right ?

Thank you very much,
Marcello



Peter Otten wrote:

>Marcello Pietrobon wrote:
>
>  
>
>>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) )
>>    
>>
>
>This is *bad*. If you want to change a list while you iterate over it, use a
>copy (there may be worse side effects than you have seen):
>          for dirname in dirs[:]:
>  
>
>>        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) )
>>    
>>
>
>You are rebinding dirs to a newly created list, leaving the old one (to
>which os.walk() still holds a reference) unaltered. Using
>
>          dirs[:] = []
>
>instead should work as desired.
>
>  
>
>>        dirs = []
>>
>>    
>>
>
>Here's what I do:
>
>def walk_files(root, recursive=False):
>    for path, dirs, files in os.walk(root):
>        for fn in files:
>            yield os.path.join(path, fn)
>            if not recursive:
>                break
>                
>Peter
>  
>






More information about the Python-list mailing list