X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FFileUtils.py;h=ac50b8a9b7a5a0d9be116664054b65a52d737e97;hb=72e0a1e1729a2c5693c94d7a835b0735f84d13ed;hp=2b89adf35365e589ef405e41dbcf3935d17222a0;hpb=11f2b70a38c7a8dbed276cd420a94e0e24bd3084;p=dead%2Fcensus-tools.git diff --git a/src/FileUtils.py b/src/FileUtils.py index 2b89adf..ac50b8a 100644 --- a/src/FileUtils.py +++ b/src/FileUtils.py @@ -27,3 +27,25 @@ def mkdir_p(path, mode): except: pass + + +def find_file_paths(root, target_filenames=[], return_first = False): + """ + Search beneath root for files whose names are contained in the + target_filenames list. 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: + for t in target_filenames: + if (f == t): + if return_first: + return [os.path.join(folder, f)] + else: + found_files.append(os.path.join(folder, f)) + + return found_files