Skip to main content

Path Tree

VCollab Path Tree - directory tree scanning and serialization using Pydantic models.

Overview

vcti-path-tree scans a directory into a tree of Pydantic models, so a directory structure can be returned from an API, stored as JSON, or printed as a CLI-style tree without ad-hoc processing at every call site. A scan produces a PathTree: a list of Directory, File and Symlink nodes, where each Directory holds its children. Links inside the scanned directory are reported but never followed, so a scan reads nothing outside it. Scans can store paths relative to a base directory, use forward slashes on every OS, limit depth, filter entries with a callback, and record modification times. Once scanned, a tree can be walked, summarized, rendered as text, and rebuilt from its serialized form.


Installation

pip install vcti-path-tree

In requirements.txt

vcti-path-tree>=2.0.0

In pyproject.toml dependencies

dependencies = [
    "vcti-path-tree>=2.0.0",
]

Quick Start

The examples below scan project_dir, a directory laid out like this:

project/
├── src/
│   ├── main.py
│   └── utils.py
└── README.md

Scan and print

from vcti.pathtree import get_cli_tree, get_path_tree

tree = get_path_tree(project_dir)

for line in get_cli_tree(tree):
    print(line)
# project/
# ├── README.md
# └── src/
#     ├── main.py
#     └── utils.py

root = tree[0]
print(root.name)  # project
print(root.type)  # directory
for child in root.children:
    print(f"  {child.name} ({child.type})")

Relative, forward-slash paths

Without base_path, every node's path keeps the form you passed in: absolute for an absolute path, relative for a relative one. Pass base_path to store paths relative to it, and as_posix=True for forward slashes on every OS:

tree = get_path_tree(project_dir, base_path=project_dir.parent, as_posix=True)
print(tree[0].path)  # project
print(tree[0].children[1].children[0].path)  # project/src/main.py

Shaping the scan

# Only the children of the scanned directory, without the root node
children = get_path_tree(project_dir, skip_root=True)

# Recurse at most one level of subdirectories below the root
shallow = get_path_tree(project_dir, max_depth=1)

# Leave out hidden entries and anything below them
visible = get_path_tree(project_dir, filter_fn=lambda p: not p.name.startswith("."))

# Record modified_time on every node
with_times = get_path_tree(project_dir, include_metadata=True)

Walk, summarize, serialize

import json

from vcti.pathtree import get_tree_stats, parse_path_tree, walk_tree

tree = get_path_tree(project_dir, base_path=project_dir.parent, as_posix=True)

for dir_path, dirs, files in walk_tree(tree):
    print(dir_path, [f.name for f in files])
# project ['README.md']
# project/src ['main.py', 'utils.py']

stats = get_tree_stats(tree)
print(stats.total_files, stats.total_dirs, stats.max_depth)  # 3 2 1

data = [node.model_dump(mode="json") for node in tree]
text = json.dumps(data, indent=2)
restored = parse_path_tree(json.loads(text))
assert restored == tree

Public API

Name Kind Description
get_path_tree(...) Function Scan a file or directory into a PathTree
handle_file(...) Function Build a File model for one file
handle_directory(...) Function Build a Directory model, recursing into children
handle_symlink(...) Function Build a Symlink model for one link, without following it
get_cli_tree(path_tree) Function Render a tree as box-drawing text lines
walk_tree(tree) Function Yield (dir_path, dirs, files) tuples, like os.walk
get_tree_stats(tree) Function Count files, directories and links, sum sizes, find max depth
parse_path_tree(data) Function Rebuild a PathTree from a list of dicts; each node's type picks its model
File Model File node: name, path, size, optional modified_time
Directory Model Directory node: name, path, children, optional modified_time
Symlink Model Link node (symbolic link or Windows junction): name, path, optional modified_time; never followed
PathBase Model Common base of the node models: name, path, type
PathType Enum FILE ("file"), DIRECTORY ("directory") or SYMLINK ("symlink")
TreeStats Model total_files, total_dirs, total_symlinks, total_size, max_depth
PathTree Type alias list[Directory | File | Symlink]
PathNode Type alias Directory | File | Symlink discriminated by type, for use in your own Pydantic models
FilterFn Type alias Callable[[Path], bool], the filter_fn signature

get_path_tree parameters

Parameter Default Effect
base_path None Store paths relative to this ancestor; when None, paths keep the form path was given in
as_posix False Use forward slashes in paths
skip_root False Return the root's children instead of the root (empty list for a file)
max_depth None Levels of subdirectories to recurse into; 0 gives the root alone; a negative value raises ValueError
filter_fn None Keep entries for which it returns True; links are passed to it too; not applied to the root
include_metadata False Populate modified_time

filter_fn is your own code and runs before the scanner classifies an entry, so it sees links too. Calls such as p.is_dir() or p.stat() read through a link to its target, which may be outside the scanned directory; decide from p.name or p.suffix, or use p.lstat(), to keep the filter inside it.

A path that does not exist gives an empty list; any other error reading it, such as a permission error, is raised. Each directory's children are sorted by name, ignoring case, in the same order on every OS.

Entries are classified without reading through them:

Entry found during the scan In the tree
Regular file File
Directory Directory, scanned recursively
Symbolic link or Windows junction Symlink, never followed; its target is not recorded
FIFO, socket, device file left out

The scanned path itself is read through if it is a link, so a data directory deployed as a link can be scanned directly. To drop links from the tree, filter them out:

no_links = get_path_tree(project_dir, filter_fn=lambda p: not p.is_symlink())

In get_cli_tree() output, links end in @, as ls -F marks them. walk_tree() lists neither links nor special files; read Directory.children to see links.


Limitations

  • Errors propagate. If an entry cannot be read, OSError is raised; there is no partial-tree or skip-on-error mode.
  • The whole tree is held in memory. There is no streaming mode.

Dependencies

  • pydantic — the tree node models and their serialization.
  • vcti-enum — the lowercase-valued PathType enum.

Documentation

If you want to… Read
Get started using the package Quick Start above
Understand the design decisions and trade-offs docs/design.md
Run a complete example examples/basic_usage.py

Metadata

Release files for vcti-path-tree 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for vcti-path-tree 2.0.0
File Size Uploaded Provenance Actions
vcti_path_tree-2.0.0.tar.gz 30.9 kB With provenance Details for vcti_path_tree-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for vcti-path-tree 2.0.0
File Interpreter ABI Platform Actions
vcti_path_tree-2.0.0-py3-none-any.whl Python 3 none any Details for vcti_path_tree-2.0.0-py3-none-any.whl

Total release size: 45.1 kB

Release files / vcti_path_tree-2.0.0.tar.gz

Download URL vcti_path_tree-2.0.0.tar.gz
Size 30.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6320fa81fbc41db0488fe1ff48c1a586714e1ea28f7205b6f14be820434835e7
BLAKE2b-256 checksum
How to use checksums
e40dd2ca4d0f540fc374364b77c24219545d2e2a53beea3509b9d14af3e80537
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.

Transparency log

Release files / vcti_path_tree-2.0.0-py3-none-any.whl

Download URL vcti_path_tree-2.0.0-py3-none-any.whl
Size 14.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cb945d41a070c623e37b9d0a702770fd5aa3d4593c6f403aab05294edc6c14d2
BLAKE2b-256 checksum
How to use checksums
d90c7b16c77b134ae03835a80b4c8a369288f39a7b0036cc938a2ddd76e90034
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.

Transparency log

Security for vcti-path-tree 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for vcti_path_tree-2.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page