def has_allowed_extension(name, allowed_file_endings=None): if not allowed_file_endings: return True return any(name.endswith(file_ending) for file_ending in allowed_file_endings) def collect_nested_files(directory_path, forbidden_dir_names=None, allowed_file_endings=None, collected_paths=None): if forbidden_dir_names is None: forbidden_dir_names = ['env', '.git', '__pycache__', 'server'] if allowed_file_endings is None: allowed_file_endings = ['.md'] if collected_paths is None: collected_paths = (directory_path, []) if any(name in directory_path.split(os.path.sep) for name in forbidden_dir_names): return collected_paths subpaths = collected_paths[1] nested_list = sorted(os.listdir(directory_path)) for name in nested_list: nested_path = os.path.join(directory_path, name) if os.path.isdir(nested_path): nested_dir = collect_nested_files(nested_path, collected_paths=(nested_path, [])) if nested_dir[1]: subpaths.append(nested_dir) else: if not has_allowed_extension(name, allowed_file_endings=allowed_file_endings): continue subpaths.append(nested_path) return collected_paths