PathStruct
A Python library for statically defining and managing file path structures. Define file and directory structures using type annotations, and visualize them as trees or manage paths.
Features
- 📁 Type-based structure definition: Define file/directory structures using class annotations
- 🌳 Tree visualization: Visualize defined structures as trees
- ✅ Existence checking: Display file/directory existence with colors
- 🔗 Path management: Convenient path access through
pathlib.Pathobjects
Why PathStruct?
When working with complex file structures, hardcoding paths throughout your codebase leads to maintenance nightmares. PathStruct solves this by allowing you to define your file structure once, statically, and reuse it everywhere.
Benefits
- 🛡️ Type Safety: Get autocomplete and type checking for all your paths. No more typos or runtime errors from incorrect path strings.
- 📝 Single Source of Truth: Define your file structure once in a class definition, and reference it consistently across your entire project.
- 🔍 Better Developer Experience: IDE autocomplete works perfectly with your path structure, making development faster and less error-prone.
- 🧹 Cleaner Code: Replace scattered string literals like
"data/config/settings.yaml"with structured access likeroot.data.config.settings_yaml.path.
Example: Before vs After
Before (hardcoded paths):
config_path = os.path.join(base_dir, "config", "settings.yaml")
log_path = os.path.join(base_dir, "logs", "app.log")
# Easy to make mistakes, no autocomplete, hard to refactor
After (with PathStruct):
class Project(Root):
config: Config
logs: Dir
root = Project(name=base_dir)
config_path = root.config.settings_yaml.path # Autocomplete works!
log_path = root.logs.app_log.path # Type-safe and refactor-friendly
Installation
Development mode installation
pip install -e .
Build distribution package
pip install build
python -m build
Deploy to PyPI
pip install twine
python -m build
twine upload dist/*
Usage
Basic usage example
from pathstruct import Root, Dir, File
# Define structure
class Config(Dir):
"""Configuration directory."""
config_yaml: File
class Dataset(Dir):
"""Dataset directory."""
log_json: File
images: Dir
class Resource(Root):
"""Resource root directory."""
config: Config
dataset: Dataset
readme_txt: File
# Create instance
root = Resource(name="/path/to/resource")
# Access paths
print(root.config.config_yaml.path)
# Output: /path/to/resource/config/config.yaml
# Print tree (with existence check)
root.print_tree(check_exist=True)
# Print tree (without colors)
root.print_tree(check_exist=False)
File extension naming convention
File fields must be named in the filename_extension format:
- ✅
config_yaml→config.yaml - ✅
readme_txt→readme.txt - ✅
log_json→log.json - ❌
config(warning will be issued)
Tree output example
/path/to/resource/
├── config/
│ └── config.yaml
├── dataset/
│ ├── log.json
│ └── images/
└── readme.txt
Existing files/directories are displayed in green, non-existing ones in red.
API Documentation
Classes
Element
Base class for all file/directory elements.
Dir
Class representing a directory.
Root
Class representing a root directory. Inherits from Dir.
File
Class representing a file. Includes extension support.
Key Methods
Element.print_tree(prefix="", is_last=True, check_exist=True)
Print directory/file structure in tree format.
Parameters:
prefix: Prefix for tree output (for indentation)is_last: Whether this is the last child nodecheck_exist: Whether to check file/directory existence and display with colors
Element.path
Returns the full path of the element as a pathlib.Path object.
Running Examples
python -m pathstruct.examples.example
Requirements
- Python 3.8 or higher
- Uses only standard library (no external dependencies)
License
MIT License
Contributing
Issues and pull requests are welcome!
Metadata
Release files for pathstruct 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pathstruct-0.1.0.tar.gz | 7.4 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pathstruct-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.8 kB
Release files / pathstruct-0.1.0.tar.gz
| Download URL | pathstruct-0.1.0.tar.gz |
|---|---|
| Size | 7.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
eb8ee03aa0e783e4d1316ed4fc37ac512448ee9ec686e44aa34c818fda5edbff
|
|
BLAKE2b-256 checksum How to use checksums |
81c766dda761a841d0132440bf41f9cc66455b0fb9204e53da9f6694e50c5168
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.5
|
Release files / pathstruct-0.1.0-py3-none-any.whl
| Download URL | pathstruct-0.1.0-py3-none-any.whl |
|---|---|
| Size | 7.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d0177b20f76f4ed7bf977d4f2edab35b0d1763a57017ee5ae772ba7da626e66f
|
|
BLAKE2b-256 checksum How to use checksums |
47509eb94371e6fe887cc5b54a0a7848fab82b10b10e890f37ffee020befcfb4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.5
|
Security for pathstruct 0.1.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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.