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.1.tar.gz (5.3 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.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: eh_extract-1.1.1.tar.gz
  • Upload date:
  • Size: 5.3 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.1.tar.gz
Algorithm Hash digest
SHA256 f29808771d9aa77b6d7d1f399320ea4d2b8a85354eacf39f78793cfdf2ed7259
MD5 84b097270d93e767491e2f3c6bdfc1e9
BLAKE2b-256 62e05534b155c421b0d112d519630107a498bd2b5df71305fbcce86240ea0611

See more details on using hashes here.

File details

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

File metadata

  • Download URL: eh_extract-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.8 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 192a272c4935d877cf0c959703ebb46356e534ec7e68c412b959aef431990025
MD5 3040f64ef6431035036526260dbd66b6
BLAKE2b-256 42dd66d290ab5dfd0b2ffe0ccc708663d303fd95668d619c0c7a6bd2c53175a6

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