From: Michael Orlitzky Date: Fri, 13 Nov 2009 01:07:11 +0000 (-0500) Subject: Added the find_file_paths function to the FileUtils module. X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=226986f4ad9de637ec22b5ee1436f0a365aaf808;p=dead%2Fcensus-tools.git Added the find_file_paths function to the FileUtils module. --- diff --git a/src/FileUtils.py b/src/FileUtils.py index 2b89adf..cf2df4e 100644 --- a/src/FileUtils.py +++ b/src/FileUtils.py @@ -27,3 +27,24 @@ def mkdir_p(path, mode): except: pass + + +def find_file_paths(root, target_filename, return_first = False): + """ + Search beneath root for files named target_filename. If + return_first is True, then return as soon as a match is + found. Otherwise, return once all matching paths have been + found. Either way, the result is a list containing all matched + paths (even if we only matched one). + """ + found_files = [] + + for folder, subfolders, files in os.walk(root): + for f in files: + if (f == target_filename): + if return_first: + return [os.path.join(folder, f)] + else: + found_files.append(os.path.join(folder, f)) + + return found_files