""" Utility functions for working with the filesystem, generally modeled after useful shell commands. """ import os def rm_f(path): """ Remove (unlink) a file, ignoring any exceptions that may be thrown. """ try: os.remove(path) except: pass def mkdir_p(path, mode): """ Create a directory hierarchy, ignoring any exceptions that may be thrown. """ try: os.makedirs(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