flattree — flatten or rebuild a directory tree with file contents. Simple CLI tool to generate or reconstruct a flat representation of a directory.
Read mode (flatten a directory):
flattree read [-o OUTPUT_FILE] [-d DIRECTORY]
[--extensions=EXT1,EXT2]
[--strict-extensions=EXT1,EXT2]
[-n MAX_LINES]
[--ignore=IGNORE1,IGNORE2]
Write mode (rebuild a directory):
flattree write -i INPUT_FILE [-d OUTPUT_DIRECTORY]
- Python ≥ 3.8
pipxinstalled (see https://pipx.pypa.io/)
pipx install "git+https://github.com/R-Elias/flattree.git"After installation, the command flattree is available globally:
flattree --helppipx upgrade flattreeflattree works in two distinct modes:
Traverses a directory and outputs a flat text file describing:
- the directory structure;
- the contents of files that match your filters.
Useful for code analysis, LLM prompts, or version snapshots.
Reads a flat file previously generated by flattree and rebuilds the full directory tree, recreating directories and files.
Existing files are overwritten by default.
readorwrite— choose mode.-d,--directory— directory to scan or restore (default: current working directory).
-o,--output— output file (default: stdout).--extensions— comma-separated list of extensions to include.--strict-extensions— same as above, but only exact matches (e.g.--strict-extensions=jsignores.spec.js).-n,--max-lines— only include file content if file ≤ N lines.--ignore— comma-separated list of files or directories to ignore.
-i,--input— flat file to rebuild from.
Flatten a directory:
flattree read -d ./my_project -o project.flatRebuild a directory from a flat file:
flattree write -i project.flat -d ./restored_projectIgnore heavy folders:
flattree read -d . --ignore=node_modules,build,dist -o output.flatInclude only .py and .js files:
flattree read --extensions=py,js -o code.flatA flat file generated by flattree is plain text divided into blocks separated by:
*----flattree-file-separator----*
Each block begins with a path — a directory (ending with /) or a file — followed by its contents if applicable.
*----flattree-file-separator----*
src/
*----flattree-file-separator----*
src/main.py
print("Hello, world!")
*----flattree-file-separator----*
README.md
# Example project
When rebuilt with:
flattree write -i example.flat -d ./restoredIt creates:
./restored/
├── src/
│ └── main.py
└── README.md
-
UTF-8 encoding is used for reading and writing.
-
Ignored patterns can also be listed in a
.flattreeignorefile. -
Default separator between entries:
*----flattree-file-separator----* -
Works on Windows, macOS, and Linux.
Elias Aliche https://github.com/R-Elias