Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from typing import Callable, List, Set, Tuple
- def is_valid_file(filename: str, valid_extensions: Tuple[str, ...] = ('.py', '.yml', '.yaml')) -> bool:
- return filename.endswith(valid_extensions)
- def write_file_content(output_file: 'TextIO', file_path: str) -> None:
- output_file.write(f"```{file_path}\n")
- with open(file_path, 'r', encoding='utf-8') as f:
- output_file.write(f.read())
- output_file.write("\n```\n\n")
- def process_files(
- root_dir: str,
- output_file: str,
- file_filter: Callable[[str], bool],
- file_processor: Callable[['TextIO', str], None],
- skip_folders: Set[str] = set()
- ) -> None:
- with open(output_file, 'w', encoding='utf-8') as out_file:
- for root, dirs, files in os.walk(root_dir):
- # Remove folders to skip from dirs to prevent os.walk from traversing them
- dirs[:] = [d for d in dirs if d not in skip_folders]
- for file in files:
- if file_filter(file):
- file_path = os.path.join(root, file)
- rel_path = os.path.relpath(file_path, root_dir) # Get relative path
- try:
- file_processor(out_file, rel_path)
- except Exception as e:
- print(f"Error processing {rel_path}: {str(e)}")
- def main(
- root_dir: str = '.',
- output_file: str = 'files.txt',
- valid_extensions: Tuple[str, ...] = ('.py', '.yml', '.yaml'),
- skip_folders: Set[str] = set()
- ) -> None:
- file_filter = lambda f: is_valid_file(f, valid_extensions)
- process_files(root_dir, output_file, file_filter, write_file_content, skip_folders)
- print(f"{output_file} has been created with the content of all {', '.join(valid_extensions)} files.")
- if skip_folders:
- print(f"Skipped folders: {', '.join(skip_folders)}")
- if __name__ == "__main__":
- main(
- root_dir='.',
- output_file='makefiles.txt',
- valid_extensions=('.py', '.yml', '.yaml'),
- skip_folders={"makefiles", 'venv', '.git', '__pycache__', 'node_modules', "python-sdk-v1", "infra", "infrastructure", "data", "trashbin", "data_science"}
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement