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).

Usage

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

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.0.0.tar.gz (5.0 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.0.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: eh_extract-1.0.0.tar.gz
  • Upload date:
  • Size: 5.0 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.0.0.tar.gz
Algorithm Hash digest
SHA256 3ac0394ddc6309f89008f9fabb47eb84a4aa56bf2e3eaaa5c70f5dfe11cf84c2
MD5 21d9a97f9160526fb4e591f9a3fd487d
BLAKE2b-256 1865d60a0fcf88996dc1210f2f0d2942d9f4f417edeefb355535cacfd4b12ad3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: eh_extract-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 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.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a1412ba2e847f0b8b581fdfc1fe3dafcd3a6e1f540d6634e4973fdef2c5c2fa3
MD5 fe4789df8e2619c52cf70efb65f2516c
BLAKE2b-256 8a861705f14f6bcc0642e42dd91ca2a40c55da3574c54f19c850067c431fed8a

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