Skip to main content

TreeDir - Directory Structure Parser and Manager

PyPI version Python Support License: MIT

A Python library for parsing directory structures from text files and implementing them in target folders with various operation modes.

Features

  • Multiple Input Formats: Support for tree format, dictionary format, and simple path format
  • Flexible Operations: Additive and strict enforcement modes
  • Visualization: Tree-style visualization of directory structures
  • Sandbox Mode: Preview changes before applying them
  • Search Functions: Find files and folders with absolute or relative paths
  • Backup Creation: Automatic backups before destructive operations
  • Cross-Platform: Works on Windows, macOS, and Linux

Installation

pip install treedir-py

Quick Start

import treedir

# Create directory structure from file (additive mode)
treedir.run('structure.txt', 'my_project')

# Strictly enforce structure (removes extra files)
treedir.urun('structure.txt', 'my_project')

# Visualize current directory
print(treedir.vis('my_project'))

# Find a file
path = treedir.find('main.py', 'my_project')

# Preview changes before applying
preview = treedir.sandbox(treedir.run, 'structure.txt', 'my_project')
print(preview)

Supported Input Formats

Tree Format

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

Dictionary Format

{
  "src": {
    "main.py": null,
    "utils.py": null
  },
  "tests": {
    "test_main.py": null
  },
  "README.md": null
}

Path Format

src/main.py
src/utils.py
tests/test_main.py
README.md

API Reference

Core Functions

run(structure_file, target="current")

Additive file system execution. Only adds new files/directories, preserves existing ones.

Parameters:

  • structure_file (str): Path to structure definition file
  • target (str): Target directory path or "current" for current directory

Returns: bool - True if successful

urun(structure_file, target="current")

Unconditional run - strictly enforce structure. Keeps common files intact but removes files not in the structure.

Parameters:

  • structure_file (str): Path to structure definition file
  • target (str): Target directory path or "current" for current directory

Returns: bool - True if successful

reset(target="current")

Reset target folder (remove all contents).

Parameters:

  • target (str): Target directory path or "current" for current directory

Returns: bool - True if successful

vis(target="current")

Visualize directory structure in tree format.

Parameters:

  • target (str): Target directory path or "current" for current directory

Returns: str - Tree representation of directory structure

find(filename, target="current")

Find file/folder and return absolute path.

Parameters:

  • filename (str): Name of file/folder to find
  • target (str): Target directory path or "current" for current directory

Returns: str or None - Absolute path if found, None otherwise

findr(filename, target="current")

Find file/folder and return relative path.

Parameters:

  • filename (str): Name of file/folder to find
  • target (str): Target directory path or "current" for current directory

Returns: str or None - Relative path if found, None otherwise

sandbox(operation_func, *args, **kwargs)

Visualize how directory will look after changes without actually executing them.

Parameters:

  • operation_func: Function to simulate (run, urun, etc.)
  • *args: Arguments for the operation function
  • **kwargs: Keyword arguments for the operation function

Returns: str - Preview of resulting directory tree

Advanced Usage

Using Classes Directly

from treedir import TreeDir, TreeParser, TreeVisualizer

# Initialize components
td = TreeDir()
parser = TreeParser()
visualizer = TreeVisualizer()

# Parse structure file
structure = parser.parse_file('my_structure.txt')

# Create structure
td._create_structure(structure, '/path/to/target', mode='additive')

# Compare two directories
comparison = visualizer.compare_structures('dir1', 'dir2')
print(comparison)

Generating Structure Files

from treedir import TreeVisualizer

tv = TreeVisualizer()

# Generate tree format from existing directory
tree_content = tv.generate_structure_file('my_project', 'tree')

# Generate dictionary format
dict_content = tv.generate_structure_file('my_project', 'dict')

# Generate path format
path_content = tv.generate_structure_file('my_project', 'path')

Error Handling

import treedir

try:
    result = treedir.run('structure.txt', 'target_folder')
    if result:
        print("Structure created successfully!")
    else:
        print("Failed to create structure")
except FileNotFoundError:
    print("Structure file not found")
except ValueError as e:
    print(f"Invalid structure format: {e}")

Examples

Example 1: Basic Project Setup

Create a project_structure.txt:

my_app/
├── src/
│   ├── __init__.py
│   ├── main.py
│   └── config.py
├── tests/
│   ├── __init__.py
│   └── test_main.py
├── docs/
│   └── README.md
├── requirements.txt
└── setup.py

Run:

import treedir

# Create the structure
treedir.run('project_structure.txt', 'new_project')

# Visualize result
print(treedir.vis('new_project'))

Example 2: Using Sandbox Mode

import treedir

# Preview what will happen
preview = treedir.sandbox(treedir.run, 'structure.txt', 'test_folder')
print("Preview:")
print(preview)

# If satisfied, apply changes
treedir.run('structure.txt', 'test_folder')

Example 3: Finding Files

import treedir

# Find a specific file
main_py_path = treedir.find('main.py', 'my_project')
if main_py_path:
    print(f"Found main.py at: {main_py_path}")

# Get relative path
rel_path = treedir.findr('main.py', 'my_project')
print(f"Relative path: {rel_path}")

Safety Features

  • Automatic Backups: urun() and reset() automatically create backups
  • Sandbox Mode: Preview changes before applying them
  • Path Validation: Validates file and directory names
  • Error Handling: Comprehensive error handling and reporting

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Changelog

Version 1.0.0

  • Initial release
  • Support for tree, dictionary, and path formats
  • Core functionality: run, urun, reset, vis, find, findr, sandbox
  • Automatic backup creation
  • Cross-platform compatibility

Metadata

Release files for treedir-py 0.1.2

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

Source distribution (sdist)

Source distribution for treedir-py 0.1.2
File Size Uploaded Provenance Actions
treedir_py-0.1.2.tar.gz 19.7 kB Without provenance Details for treedir_py-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for treedir-py 0.1.2
File Interpreter ABI Platform Actions
treedir_py-0.1.2-py3-none-any.whl Python 3 none any Details for treedir_py-0.1.2-py3-none-any.whl

Total release size: 35.8 kB

Release files / treedir_py-0.1.2.tar.gz

Download URL treedir_py-0.1.2.tar.gz
Size 19.7 kB
Tags Source
SHA-256 checksum
How to use checksums
69f08b8cbcfc951df3b9e342f2881a568d8b34d33e0020a89aea189c4cd3f530
BLAKE2b-256 checksum
How to use checksums
900dc3f1f3a6a5fd9094fb915af8bbe28fa6b26fae31d4c2fa714ee9859dffaa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.9

Release files / treedir_py-0.1.2-py3-none-any.whl

Download URL treedir_py-0.1.2-py3-none-any.whl
Size 16.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e9c70a7d113fc3a8ab990a0cc0911fad2b9f9353709e598f353ac8c5a91fcd08
BLAKE2b-256 checksum
How to use checksums
d81c747f607a6a7bc2fa071e0d887fe0f8aecb5e16ab1253dc2e0979e24bb1ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.9

Security for treedir-py 0.1.2

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.

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files No 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