[Tutor] Problem with os.walk

Isr Gish isrgish at fastem.com
Tue May 11 00:16:17 EDT 2004

The docs say that you can del the dirnames list. But when I tried doing it, it didn't work. 
Here is the help info:

Help on function walk in module os:

walk(top, topdown=True, onerror=None)
    Directory tree generator.
    For each directory in the directory tree rooted at top (including top
    itself, but excluding '.' and '..'), yields a 3-tuple
        dirpath, dirnames, filenames
    dirpath is a string, the path to the directory.  dirnames is a list of
    the names of the subdirectories in dirpath (excluding '.' and '..').
    filenames is a list of the names of the non-directory files in dirpath.
    Note that the names in the lists are just names, with no path components.
    To get a full path (which begins with top) to a file or directory in
    dirpath, do os.path.join(dirpath, name).
    If optional arg 'topdown' is true or not specified, the triple for a
    directory is generated before the triples for any of its subdirectories
    (directories are generated top down).  If topdown is false, the triple
    for a directory is generated after the triples for all of its
    subdirectories (directories are generated bottom up).
    When topdown is true, the caller can modify the dirnames list in-place
    (e.g., via del or slice assignment), and walk will only recurse into the
    subdirectories whose names remain in dirnames; this can be used to prune
    the search, or to impose a specific order of visiting.  Modifying
    dirnames when topdown is false is ineffective, since the directories in
    dirnames have already been generated by the time dirnames itself is

I tried del and dirnames = dirnames[0:0]
Both didn't work. The only thing that wOrked was.
for d in dirnames[:]:

And if I didn't make a copy it didn't work correctly. (it seems that for i in list works with indexing behindethe seigns).

All help in this will be appreciated.

All the best

More information about the Tutor mailing list