def ensure_directories_end_in_separator(opts_dict): """ Adds a path separator to the end of the input and output directories, if they don't already have them. :param opts_dict: dictionary that will be modified """ assert_has_input_output(opts_dict) assert "is_dir" in opts_dict if opts_dict["is_dir"]: opts_dict["input"] = add_suffix(opts_dict["input"], get_path_separator()) opts_dict["output"] = add_suffix(opts_dict["output"], get_path_separator())
def test_add_suffix(): """ normalize_ops.py: Test add_suffix() add_suffix() should add a suffix to a string, unless that string already ends with the suffix. """ a = 'test' assert add_suffix(a, 'cow') == 'testcow' b = 'this_has_suffix.md' assert add_suffix(b, '.md') == b
def ensure_directories_end_in_separator(opts_dict): """ Adds a path separator to the end of the input and output directories, if they don't already have them. :param opts_dict: dictionary that will be modified """ assert_has_input_output(opts_dict) assert 'is_dir' in opts_dict if opts_dict['is_dir']: opts_dict['input'] = add_suffix(opts_dict['input'], get_path_separator()) opts_dict['output'] = add_suffix(opts_dict['output'], get_path_separator())
def get_files(dir, exts, exclude=None, recursive=False): """ Get a list of files within a directory with given extensions. Exclude/black list directories from the list if specified. By default, the search is recursive, looking in all subdirectories of 'dir' :param dir: String root directory to search under. All subdirectories are also searched by default :param exts: List of string file extensions. Files inside of dir with names ending with this string will be included in the list. Note: the string does not strictly need to be a file extension (beginging with a '.' dot), it could be the entire name of the file, or a common suffix to files. :param exclude: List of strings specifying directories that should not be included in the output list :param recursive: When True, search in all subdirectories, otherwise just look in the current directory :return: List of string directories """ file_paths =[] if recursive: for root, _, _ in os.walk(dir): # os.walk() does not add path separator by default to end of path root = add_suffix(root, get_path_separator()) if exclude is not None and is_dir_inside(root, exclude): # Skip directories that are in the exclude list continue file_paths.extend(get_files_in_dir(root, *exts)) else: file_paths.extend(get_files_in_dir(dir, *exts)) return file_paths
def get_files(dir, exts, exclude=None, recursive=False): """ Get a list of files within a directory with given extensions. Exclude/black list directories from the list if specified. By default, the search is recursive, looking in all subdirectories of 'dir' :param dir: String root directory to search under. All subdirectories are also searched by default :param exts: List of string file extensions. Files inside of dir with names ending with this string will be included in the list. Note: the string does not strictly need to be a file extension (beginging with a '.' dot), it could be the entire name of the file, or a common suffix to files. :param exclude: List of strings specifying directories that should not be included in the output list :param recursive: When True, search in all subdirectories, otherwise just look in the current directory :return: List of string directories """ file_paths = [] if recursive: for root, _, _ in os.walk(dir): # os.walk() does not add path separator by default to end of path root = add_suffix(root, get_path_separator()) if exclude is not None and is_dir_inside(root, exclude): # Skip directories that are in the exclude list continue file_paths.extend(get_files_in_dir(root, *exts)) else: file_paths.extend(get_files_in_dir(dir, *exts)) return file_paths