Skip to main content

Lightweight Python utilities to render directory trees and extract file contents with filtering.

Project description

treeproject

treeproject is a lightweight Python library providing simple, deterministic utilities to:

  • render a directory structure as a readable Unicode tree,
  • extract and concatenate file contents into a single structured text bundle.

It is designed for documentation, debugging, tooling, and building compact filesystem context bundles (for example, for LLM prompts).

The API is intentionally minimal, dependency-free, and based on pathlib.Path.


Installation

pip install treeproject

Requires Python 3.10+.


Features

  • Unicode tree rendering (similar to the Unix tree command)
  • Deterministic traversal (directories first, case-insensitive sorting)
  • Strict pruning-based filtering
  • Optional symbolic link traversal
  • Text content extraction with stable formatting
  • Binary file detection and skipping
  • Configurable encoding and error handling
  • Zero runtime dependencies

Quick Start

Print a directory tree

from pathlib import Path
from treeproject import print_tree

print_tree(Path("./my_project"))

Example output:

my_project
├── README.md
├── pyproject.toml
└── src
    ├── __init__.py
    └── app.py

Extract file contents

from pathlib import Path
from treeproject import path_content

bundle = path_content(Path("./my_project"))
print(bundle)

Example output format:

===== FILE: README.md =====
# My Project
...
===== END FILE =====

===== FILE: src/app.py =====
print("hello")
===== END FILE =====

Filtering and Pruning

Filtering is controlled via an include(Path) -> bool predicate.

If the predicate returns False for a directory, the directory is pruned and none of its descendants are visited.

IGNORE = {".git", "__pycache__", ".pytest_cache"}

def include(p: Path) -> bool:
    return p.name not in IGNORE

print_tree(Path("."), include=include)

The same predicate can be reused for content extraction.


API Reference

print_tree(root, *, follow_symlinks=False, include=lambda p: True) -> None

Print a Unicode directory tree to standard output.

  • Directories are listed before files
  • Sorting is case-insensitive
  • Excluded directories are fully pruned

`path_content(root, *, follow_symlinks=False, include=lambda p: True,

skip_binary=True, encoding="utf-8", errors="raise") -> str`

Walk a filesystem path and return a single string containing the formatted contents of all selected files.

  • Files are processed in deterministic order
  • Binary files can be skipped automatically
  • Errors can be raised or ignored per file

Design Notes

  • Uses pathlib.Path.walk for traversal
  • No global state
  • No side effects except explicit printing in print_tree
  • Suitable for programmatic use and automation

Development

git clone https://github.com/dylan-lebreton/treeproject
cd treeproject
poetry install
pytest

License

MIT License

Copyright (c) 2025 Dylan Lebreton

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

treeproject-1.0.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

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

treeproject-1.0.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: treeproject-1.0.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.12.12 Linux/6.12.5-linuxkit

File hashes

Hashes for treeproject-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e8d14a54f3c2333fa7d27584cb2033ded1a5dc2a385bf4e1d6354ebde44b7e3e
MD5 6b3a7be1c48dc3a23727372aaa1ee519
BLAKE2b-256 14c5dbdc4a1823a46361541679493543cbda535716eb305e5dda4c6e172b3165

See more details on using hashes here.

File details

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

File metadata

  • Download URL: treeproject-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.12.12 Linux/6.12.5-linuxkit

File hashes

Hashes for treeproject-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c4581d2dc59f517f0f7337d2c21408d983121466a2cec2e28a282910a3f63f31
MD5 a49d3ae0b24c2564d65a0b358bdfcda1
BLAKE2b-256 57d4032ae8b1e32fc65a55ca8ffd552f889b71dbbb3db112cf9761b722cb5082

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