Skip to main content

Convert code files to a single text file for LLM consumption

Project description

CodeToTxt

A powerful Python package to convert code files into a single text file, perfect for feeding into Large Language Models (LLMs) or for easy code review and documentation.

Features

Core Features:

  • ๐Ÿ“ Convert entire directories of code into a single text file
  • ๐ŸŒณ Optional directory tree visualization
  • ๐Ÿšซ Respects .gitignore patterns automatically
  • ๐ŸŽจ Customizable file separators and output format
  • ๐Ÿ”ง Flexible file filtering by extension or glob patterns
  • ๐Ÿ“ฆ Easy to use CLI and Python API

Installation

pip install code-to-txt

Or with Poetry:

poetry add code-to-txt

Quick Start

Basic Usage

# Show version
code-to-txt --version

# Convert all code files with timestamp
code-to-txt -t

# Preview what would be processed
code-to-txt --dry-run

# Get codebase statistics
code-to-txt --stats

# Convert specific directory
code-to-txt ./my-project -o project.txt

# Copy to clipboard instead of saving
code-to-txt --clipboard-only

Specify File Types

# Multiple extensions (space or comma separated)
code-to-txt -e ".py .js .ts"
code-to-txt -e ".py,.js,.ts"

# Using glob patterns
code-to-txt -g "*.py" -g "src/**/*.js"
code-to-txt -g "*.py" -g "*.md"

Advanced Usage

# Limit file sizes (useful for LLM token limits)
code-to-txt --max-file-size 500

# Exclude patterns
code-to-txt -x "tests/*" -x "*.test.js"

# Don't use .gitignore
code-to-txt --no-gitignore

# Don't show directory tree
code-to-txt --no-tree

# Custom separator
code-to-txt --separator "---"

# Combine options
code-to-txt -t -c -e ".py .js" -x "tests/*"

Configuration File

Create a default configuration file:

code-to-txt --init-config

This creates .code-to-txt.yml with default settings:

# Output file name
output: code-to-txt.txt

# File extensions to include (null = use defaults)
extensions: null

# Patterns to exclude
exclude:
  - "tests/*"
  - "*.test.js"
  - "*.test.ts"
  - "*.spec.js"
  - "*.spec.ts"
  - "node_modules/*"
  - "__pycache__/*"
  - "*.pyc"

# Glob patterns (alternative to extensions)
glob: [ ]

# Options
no_gitignore: false
no_tree: false
separator: "================"
clipboard: false
clipboard_only: false
timestamp: false
max_file_size: null

Use the config file:

code-to-txt --config .code-to-txt.yml

Note: CLI arguments override config file settings.

Example Configurations

Python Project:

extensions: [ .py ]
exclude: [ "tests/*", "*.pyc", "__pycache__/*", "venv/*", ".venv/*" ]
timestamp: true
max_file_size: 500

JavaScript/TypeScript Project:

extensions: [ .js, .ts, .jsx, .tsx ]
exclude: [ "node_modules/*", "dist/*", "build/*", "*.test.js", "*.spec.ts" ]
no_tree: false
max_file_size: 1000

LLM-Optimized:

extensions: [ .py, .js, .md ]
exclude: [ "tests/*", "*.test.*", "node_modules/*", "dist/*", "build/*" ]
timestamp: true
clipboard: true
max_file_size: 200
no_tree: false

Command Line Options

Usage: code-to-txt [OPTIONS] [PATH]

Arguments:
  PATH                    Directory to scan (default: current directory)

Options:
  -o, --output PATH       Output file path (default: codetotxt_YYYYMMDD_HHMMSS.txt)
  -e, --extensions TEXT   File extensions to include (space or comma separated)
  -x, --exclude TEXT      Patterns to exclude (can be used multiple times)
  -g, --glob TEXT         Glob patterns to include (can be used multiple times)
  --no-gitignore          Don't respect .gitignore files
  --no-tree               Don't include directory tree in output
  --separator TEXT        Separator between files
  -c, --clipboard         Copy output to clipboard in addition to file
  --clipboard-only        Copy to clipboard only (don't save file)
  --config PATH           Path to config file (.yml or .yaml)
  --init-config           Create default configuration file
  -t, --timestamp         Add timestamp to output filename
  -v, --version           Show version and exit
  --dry-run               Show which files would be processed
  --stats                 Show detailed statistics
  --max-file-size INT     Skip files larger than N KB
  --help                  Show this message and exit

Python API

Basic Usage

from code_to_txt import CodeToText

code_to_txt = CodeToText(
    root_path="./my-project",
    output_file="output.txt",
    include_extensions={".py", ".js"},
)

num_files = code_to_txt.convert(add_tree=True)
print(f"Processed {num_files} files")

Generate Content for Clipboard

from code_to_txt import CodeToText
import pyperclip

code_to_txt = CodeToText(
    root_path="./my-project",
    output_file=None,
    include_extensions={".py"},
)

content = code_to_txt.generate_content(add_tree=True)
pyperclip.copy(content)

Get Statistics

from code_to_txt import CodeToText

code_to_txt = CodeToText(
    root_path="./my-project",
    output_file=None,
    max_file_size_kb=500,
)

stats = code_to_txt.calculate_statistics()
print(f"Total files: {stats['total_files']}")
print(f"Total size: {stats['total_size_bytes'] / 1024 / 1024:.2f} MB")
print(f"Total lines: {stats['total_lines']:,}")

Using Glob Patterns

from code_to_txt import CodeToText

code_to_txt = CodeToText(
    root_path="./my-project",
    output_file="output.txt",
    glob_patterns=["*.py", "src/**/*.js", "**/*.md"],
)

num_files = code_to_txt.convert()

Default File Extensions

When no extensions are specified, CodeToTxt includes these file types by default:

  • Python: .py
  • JavaScript/TypeScript: .js, .ts, .jsx, .tsx
  • Systems: .c, .cpp, .h, .hpp, .java, .cs, .go, .rs
  • Web: .html, .css, .scss
  • Config: .yaml, .yml, .json, .toml, .xml
  • Documentation: .md, .txt, .rst
  • Scripts: .sh, .bash, .zsh
  • Other: .rb, .php, .swift, .kt, .scala, .r, .sql

Default Ignore Patterns

CodeToTxt automatically ignores common build artifacts and dependencies:

  • __pycache__, *.pyc, *.pyo, *.pyd
  • .git, .svn, .hg
  • node_modules
  • .venv, venv, .env
  • *.egg-info, dist, build
  • .pytest_cache, .mypy_cache, .ruff_cache
  • *.so, *.dylib, *.dll

Plus any patterns in your .gitignore file (including parent directories).

Output Format

The generated file includes:

  1. Header: Source directory and file count
  2. Directory Tree: Visual representation of the file structure (optional)
  3. File Contents: Each file with its relative path and content

Example output:

Code Export from: /path/to/project
Total files: 4
================================================================================

DIRECTORY TREE:
================================================================================
my-project/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ main.py
โ”‚   โ””โ”€โ”€ utils.py
โ”œโ”€โ”€ tests/
โ”‚   โ””โ”€โ”€ test_main.py
โ””โ”€โ”€ README.md

================================================================================

FILE 1/4: src/main.py
================================================================================
def main():
    print("Hello, World!")

if __name__ == "__main__":
    main()

================================================================================
...

Use Cases

  • ๐Ÿ“š Code Review: Share entire codebase in a single file
  • ๐Ÿค– LLM Input: Feed code to ChatGPT, Claude, or other AI assistants
  • ๐Ÿ“– Documentation: Create comprehensive code documentation
  • ๐Ÿ” Code Search: Easy text-based search across entire project
  • ๐Ÿ“Š Analysis: Input for code analysis tools
  • ๐Ÿ’พ Archival: Simple code backup format

Tips & Tricks

For LLM Consumption

# Step 1: Check what you're working with
code-to-txt --stats

# Step 2: Preview files
code-to-txt --dry-run --max-file-size 200

# Step 3: Copy to clipboard with size limit
code-to-txt --clipboard-only --max-file-size 200 -e ".py .md"

# See token estimate:
# Estimated tokens: ~95,000

For Large Projects

# Use specific extensions to reduce size
code-to-txt -e ".py" -t --max-file-size 500

# Exclude heavy directories
code-to-txt -x "node_modules/*" -x "venv/*" -x "dist/*"

# Get statistics first
code-to-txt --stats --max-file-size 300

Debug Ignore Patterns

# See which files are being skipped and why
code-to-txt --dry-run

# Compare with and without gitignore
code-to-txt --dry-run --no-gitignore

Requirements

  • Python 3.10+
  • Dependencies: click, gitpython, pathspec, pyperclip, pyyaml

Development

# Clone repository
git clone https://github.com/AndriiSonsiadlo/code-to-txt.git
cd code-to-txt

# Install with Poetry
poetry install

# Run tests
poetry run pytest

# Run linting
poetry run ruff check .
poetry run mypy src/

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License - see LICENSE file for details.

Changelog

v0.3.0

  • ๐Ÿ”ง Refactored codebase for better maintainability
  • ๐Ÿ“ Externalized default extensions and ignore patterns to separate files
  • ๐Ÿ› Fixed critical gitignore bug (now checks parent directories)
  • ๐Ÿ” Improved cross-platform path handling
  • ๐Ÿ“Š Added --stats flag for detailed codebase statistics
  • ๐ŸŽฏ Added --dry-run mode to preview without processing
  • ๐Ÿ“ Added --max-file-size to skip large files
  • ๐Ÿ”ข Added token estimation for LLM consumption
  • ๐Ÿ“ Added skip tracking to see which files were excluded
  • ๐Ÿš€ Improved method naming and code structure
  • โœ… Enhanced test coverage

v0.2.0

  • โœจ Added automatic timestamp generation for output files
  • ๐Ÿ“‹ Added clipboard support (--clipboard and --clipboard-only)
  • ๐ŸŽฏ Improved extension handling (space/comma separated)
  • ๐Ÿ” Added glob pattern support
  • โš™๏ธ Added configuration file support (.code-to-txt.yml)
  • ๐Ÿš€ Expanded default file extensions and ignore patterns
  • ๐Ÿ› Various bug fixes and improvements

v0.1.0

  • ๐ŸŽ‰ Initial release
  • ๐Ÿ“ Basic directory to text conversion
  • ๐ŸŒณ Directory tree generation
  • ๐Ÿšซ .gitignore support
  • ๐ŸŽจ Customizable separators

Acknowledgments

Created by Andrii Sonsiadlo

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

code_to_txt-0.3.0.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

code_to_txt-0.3.0-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file code_to_txt-0.3.0.tar.gz.

File metadata

  • Download URL: code_to_txt-0.3.0.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.10.19 Linux/6.11.0-1018-azure

File hashes

Hashes for code_to_txt-0.3.0.tar.gz
Algorithm Hash digest
SHA256 114797083c234122ad8dc9fb356923e07d462e636796df8eaf907d6528ad80d2
MD5 8b72f49e290715045363f41fef083d63
BLAKE2b-256 e7403a82d3fc3a271221eb5be517f97efb14ce2741c2e6107949c95889881820

See more details on using hashes here.

File details

Details for the file code_to_txt-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: code_to_txt-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 14.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.10.19 Linux/6.11.0-1018-azure

File hashes

Hashes for code_to_txt-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d9a1b6c7cbc8c5a6c28d49f401e610fde8daaf6039b4ec2289ab398f9c3f5b05
MD5 8cc0bd5a23b3407287203606233173b6
BLAKE2b-256 475b8cb7d340deb7523dadf4fcc559fe71dd921163057d628608dd8204dbdedf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page