A simple Python utility that collects and combines code files from a directory into a single text file, optimized for sharing with Large Language Models (LLMs).
Code Collector scans a specified directory, gathers all code files while respecting .gitignore patterns, and creates a single output file. This is particularly useful when you need to share multiple files with an LLM for code reviews, refactoring suggestions, or other tasks that require contextual understanding of your codebase.
- Respects
.gitignorepatterns to skip irrelevant files - Formats output with clear file headers for easy navigation
- Includes a directory tree structure at the end of the file
- Handles encoding issues gracefully
- Creates output in your current working directory, not in the scanned directory
- Python 3.6+
pathspecpackage for parsing gitignore patternstreecommand (optional, for directory structure visualization)
-
Install required Python package:
pip install pathspec -
(Optional) Install the
treecommand:- On Ubuntu/Debian:
apt-get install tree - On macOS with Homebrew:
brew install tree - On Windows: Available through various package managers like Chocolatey
- On Ubuntu/Debian:
python3 ./get-code.py <directory_path>
Example:
python3 ./get-code.py ~/projects/my-app
This will create a combined_output.txt file in your current directory containing all code from ~/projects/my-app.
The output file contains:
-
Each file's content with a header:
<FILENAME: path/to/file.js> [file content here] ################################################################################ -
A directory tree structure at the end (if
treecommand is available)
- For large codebases, consider running the script on specific subdirectories to keep outputs within token limits
- When sharing with an LLM, explain what you're looking for (e.g., code review, bug identification, refactoring suggestions)
- Reference specific filenames when asking questions about the code
- For very large outputs, you may need to chunk the file into smaller pieces
- Very large codebases may exceed LLM token limits