View Single Post
Old Jun 28th, 2006, 2:19 PM   #7
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
As an aside, there's a lot of common functionality in your two functions that can be abstracted out. If there was a function similar to os.walk, but with a recursive limit, then your functions would be a lot simpler.

Maybe something like this:
def limited_walk(path, limit, n = 0):
    if n > limit: return
                    
    for file in os.listdir(path):
        file_path = os.path.join(path, file)
            
        if os.path.isdir(file_path):
            for item in limited_walk(file_path, limit, n + 1):
                yield item
        else:
            yield file_path

# Example of use:
for filename in limited_walk("some_directory", 4):
    if os.path.basename(filename) == name:
        print filename
Arevos is offline   Reply With Quote