Got it! You want a Python script that recursively walks through a Git repository, finds all .py files, and writes them into a single Markdown file in a tree-like structure, followed by the file contents. Here's a clean script to do that:
import os
def generate_tree_md(root_dir, md_file):
python_files = []
# Walk the directory recursively and collect Python files
for dirpath, dirnames, filenames in os.walk(root_dir):
for filename in filenames:
if filename.endswith('.py'):
full_path = os.path.join(dirpath, filename)
relative_path = os.path.relpath(full_path, root_dir)
python_files.append(relative_path)
# Sort files to ensure tree structure is consistent
python_files.sort()
with open(md_file, 'w', encoding='utf-8') as f:
last_parts = []
for file_path in python_files:
parts = file_path.split(os.sep)
# Print tree structure
for i, part in enumerate(parts):
if i < len(parts) - 1: # Directory part
if len(last_parts) <= i or last_parts[i] != part:
# Print directory if it changed
f.write(' ' * i + f'└── {part}\n')
if len(last_parts) <= i:
last_parts.append(part)
else:
last_parts[i] = part
else: # File part
f.write(' ' * i + f'├── {part}\n')
# Append file content
f.write('```\n')
with open(os.path.join(root_dir, file_path), 'r', encoding='utf-8') as code_file:
f.write(code_file.read())
f.write('```\n\n')
if __name__ == "__main__":
repo_path = "." # Change this to your repo path
output_md = "merged_python_files.md"
generate_tree_md(repo_path, output_md)
print(f"Merged Python files written to {output_md}")
✅ How it works:
- Recursively collects all
.pyfiles in the repo. - Sorts them to maintain tree order.
- Writes a tree-like Markdown representation of directories and files.
- Appends the content of each Python file in code blocks.
If you want, I can also improve this to look exactly like the Linux tree command with proper ├── and └── for both files and folders for nested directories—it’ll look prettier and more accurate for big projects.
Do you want me to do that?