os.path.walk not pruning descent tree (and I'm not happy with that behavior?)

Joe Ardent ardent at gmail.com
Sun May 27 21:39:32 EDT 2007


Good day, everybody!  From what I can tell from the archives, this is
everyone's favorite method from the standard lib, and everyone loves
answering questions about it.  Right? :)

Anyway, my question regards the way that the visit callback modifies
the names list.  Basically, my simple example is:

##############################
def listUndottedDirs( d ):
    dots = re.compile( '\.' )

    def visit( arg, dirname, names ):
        for f in names:
            if dots.match( f ):
                i = names.index( f )
                del names[i]
            else:
                print "%s: %s" % ( dirname, f )

    os.path.walk( d, visit, None )
###############################

Basically, I don't want to visit any hidden subdirs (this is a unix
system), nor am I interested in dot-files.  If I call the function
like, "listUndottedDirs( '/usr/home/ardent' )", however, EVEN THOUGH
IT IS REMOVING DOTTED DIRS AND FILES FROM names, it will recurse into
the dotted directories; eg, if I have ".kde3/" in that directory, it
will begin listing the contents of /usr/home/ardent/.kde3/ .  Here's
what the documentation says about this method:

"The visit function may modify names to influence the set of
directories visited below dirname, e.g. to avoid visiting certain
parts of the tree. (The object referred to by names must be modified
in place, using del or slice assignment.)"

So...  What am I missing?  Any help would be greatly appreciated.


--
Joe Ardent




More information about the Python-list mailing list