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.