Problem with directory recursion!

Robert Dailey rcdailey at gmail.com
Fri Aug 17 14:16:32 EDT 2007


Hi,

I've created a function that is used to recurse a directory tree in Windows
XP using os.walk(). For the most part it works, however in some instances
the data is incorrect and I'm getting invalid sub-directory paths. Here's
the function:


def __doSearch( root_dir, sub_path, restype, ext ):
    print sub_path
    # Searches the specified directory and generates a
    # list of files to preload.
    complete_path = osp.normpath( osp.join( root_dir, sub_path ) )
    for root, dirs, files in os.walk( complete_path ):

        # Record the list of file hash ID's
        for file in files:
            split = __resType( file )
            if split[1] == restype:
#                print sub_path
                print "\t", file
                __appendResource( ext, sub_path, split[0] )

        # Remove .svn subdirectories; we don't walk these.
        if ".svn" in dirs:
            dirs.remove( ".svn" )

        # Recurse child directories
        for dir in dirs:
            __doSearch( root_dir, osp.join( sub_path, dir ), restype, ext )

Does anyone see anything immediately suspicious about the code? I'm assuming
that in Python, every time a function is called recursively it gets its own
copy of local variables. For some reason the sub_path variable isn't
consistent depending on where I use it. For example, if you notice the print
call at the very start of the function, it will output something like
"models/ships". However, after passing it into __appendResource(), it
appears to be just "models".
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070817/a2ca6aa8/attachment.html>


More information about the Python-list mailing list