os.walk walks too much

Peter Otten __peter__ at web.de
Wed Feb 25 08:34:56 EST 2004


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