View Single Post
Old Jan 12th, 2006, 3:11 AM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
You need to have some sort of limiting condition:
def file_search(search_file, path):
    for filename in os.listdir(path):
        full_filename = os.path.join(path, filename)

        if filename == search_file:
            return full_filename

        if os.path.isdir(full_filename) and not os.path.islink(full_filename):
            found_file = file_search(search_file, full_filename)
            if found_file != None:
                return found_file

    return None
Once the file is found, the filename is returned. If a file_search instance detects that one of its children has received a filename that is not None, it returns also, thus breaking the recursion.
Arevos is offline   Reply With Quote