Skip to main content

A CLI tool to extract file contents and structure into a single text file.

Project description

🗂️ Extract Files to Text Utility

This Python script (extract_files.py) recursively extracts the contents of all files in a given directory and writes them to a single text file. It also optionally generates a “tree-like” structure (similar to the Unix tree -fa command) at the top of the output file.

Useful for:

  • Code auditing or review

  • AI context extraction (feeding codebases into LLMs)

  • Creating a readable snapshot of project files and structure


📁 Features

  • ✅ Generates a tree -fa-like structure for the repository

  • ✅ Recursively extracts files and writes them to a single .txt file

  • ✅ Supports inclusion/exclusion by file extensions, directories, and filenames

  • ✅ Handles UTF-8 encoding safely with error catching

  • ✅ Works on Linux, macOS, and Windows


🧰 Requirements

  • Python 3.7+

  • No external dependencies (only uses the standard library)


⚙️ Usage

1. Clone or copy this script

git clone https://github.com/EsmailEbrahim/eh-extract-files-to-txt.git
cd eh-extract-files-to-txt

2. Run the script

Edit the repo_directory and output_txt_file variables in the script or pass them as arguments.

Example inside the script:

if __name__ == "__main__":
    repo_directory = r"/path/to/your/project"
    output_txt_file = r"/path/to/output/project_extract.txt"

    extract_files_to_txt(
        repo_directory,
        output_txt_file,
        include_ext=['.py', '.yml', '.yaml', '.json', '.sh', '.env', '.txt'],
        exclude_dirs=['.git', '__pycache__', 'venv', 'node_modules', 'dist', 'build', 'uploads', 'logs', 'certs'],
        exclude_files=['README.md', 'LICENSE'],
        include_tree=True
    )

    print(f"File paths, contents, and structure have been written to {output_txt_file}")

Then run:

python extract_files.py

🧩 Function Overview

generate_tree(repo_path)

Generates a string representing a tree structure of the repository.

extract_files_to_txt(...)

Main function that:

  • Walks the given directory

  • Filters files and folders based on inclusion/exclusion lists

  • Writes file paths and their contents to an output text file


Example Output

Repository structure for: /home/esmail/my-repo
================================================================================
├── my-repo/
│   ├── main.py
│   ├── utils/
│   │   ├── helpers.py
================================================================================

main.py:
print("Hello world")

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


🧾 License

This project is licensed under the MIT License.


👨‍💻 Author

Esmail Ebrahim Hamza


💻 Quick Install & Usage

Install from PyPI

pip install eh-extract

Or directly from GitHub

pip install git+https://github.com/EsmailEbrahim/eh-extract-files-to-txt.git

🧠 Windows Users:
If you see a warning like
The script eh-extract.exe is installed in ... which is not on PATH,
add that folder to your system PATH (usually C:\Users\<User>\AppData\Roaming\Python\Python312\Scripts).

On Ubuntu (safe install via pipx)

sudo apt install pipx
pipx ensurepath
pipx install eh-extract
eh-extract -i /path/to/repo -o /path/to/output.txt

Usage

eh-extract -i /path/to/repo -o /path/to/output.txt --include-ext .py .yml .json

🆕 Changelog

v1.1.0 (2025-11-13)

  • Added --exclude-tree-dirs and --exclude-tree-files options.
  • These allow excluding directories or files from the generated tree structure separately from content extraction.

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

eh_extract-1.1.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

eh_extract-1.1.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file eh_extract-1.1.0.tar.gz.

File metadata

  • Download URL: eh_extract-1.1.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for eh_extract-1.1.0.tar.gz
Algorithm Hash digest
SHA256 8d1826a570553b9ff0b44804fea41a8ce0ee61b469ecebe9b346f6a6bc3ea484
MD5 c29b0ae85e46b64793bf3cf9235bafdc
BLAKE2b-256 be6a8bb3f78340bd5f068bd5e1534565f91c306dc3954daee7e4b87a5c1f3b8b

See more details on using hashes here.

File details

Details for the file eh_extract-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: eh_extract-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for eh_extract-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 330d7bf199a343cdcbee8f5221462590954f6872adc1adcf288604a3691db7d6
MD5 8ebb47fc73602bc52a3c78dfdcdcce11
BLAKE2b-256 9653524de8f42a7266d8412485e54ab757353908a7e08ad2a49154bc0bfd8572

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