Hi,<br><br>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:
<br><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">def __doSearch( root_dir, sub_path, restype, ext ):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> print sub_path</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> # Searches the specified directory and generates a
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> # list of files to preload.</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
complete_path = osp.normpath( osp.join( root_dir, sub_path ) )</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> for root, dirs, files in os.walk( complete_path ):
</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> # Record the list of file hash ID's</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> for file in files:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> split = __resType( file )</span>
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if split[1] == restype:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
# print sub_path</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> print "\t", file</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> __appendResource( ext, sub_path, split[0] )</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
# Remove .svn subdirectories; we don't walk these.</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if ".svn" in dirs:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> dirs.remove( ".svn" )</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
# Recurse child directories</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> for dir in dirs:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
__doSearch( root_dir, osp.join( sub_path, dir ), restype, ext )<br><br></span><span>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".
</span><span style="font-family: courier new,monospace;"><br></span>