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

Render a directory tree

from pathlib import Path
from treeproject import path_tree

print(path_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(path_tree(Path("."), include=include))

The same predicate can be reused for content extraction.


API Reference

path_tree(root, *, follow_symlinks=False, include=lambda p: True) -> str

Render a Unicode directory tree and return it as a string.

  • 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 string rendering in path_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-2.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-2.0.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: treeproject-2.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-2.0.0.tar.gz
Algorithm Hash digest
SHA256 b07e9cd91f41faaa5122ec951421fd0bc2d56a161a84b90a591af6abda7552e1
MD5 94fee5abf661b0b04ad3383fe13e3a71
BLAKE2b-256 1c556cf11e2d8ad048aa406662d4020998993d0f93f6fd331c05e2b10a5a426e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: treeproject-2.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-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6fe88c9086655af84ab5c9510b2196b8f1100ad85f529237e57663c022fb7b76
MD5 f59ffb1ddfb59dccfb8ab41c81900338
BLAKE2b-256 5a766b318d226e58ddadca41faaf5900900dda5ba33ae1958984b7a133d990be

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