DEV Community

Cover image for Building and Publishing tree2fs: A Python Package for Folders/Files Generation
Abdellah Hallou
Abdellah Hallou

Posted on

Building and Publishing tree2fs: A Python Package for Folders/Files Generation

The Problem

Every developer has been there: you're reading documentation, see a beautiful project structure, and think "I'll just recreate this quickly."

Fifteen minutes later, you're still typing mkdir and touch, you've made three typos, and you're wondering why this isn't automated.

I was working on a project when I realized I'd recreated the same project structure three times in one week (I don't use LLMs when I code). That was the moment I decided to build tree2fs.

What is tree2fs?

tree2fs is a Python package that converts tree-formatted text into actual filesystem structures.

Instead of manually creating each folder and file, you write:

project/
├── src/
│   ├── main.py
│   └── utils.py
├── tests/
└── README.md
Enter fullscreen mode Exit fullscreen mode

And run:

tree2fs tree.txt
Enter fullscreen mode Exit fullscreen mode

Done. Your entire structure is created.

Architecture Decisions

1. Two-Phase Architecture

Separate Parsing from Filesystem Operations

Text File → Parser → Tree Data Structure → Builder → Filesystem
Enter fullscreen mode Exit fullscreen mode

2. Tree Data Structure

Use Proper Tree Nodes Instead of Path Stacks

class Node:    
    def __init__(self, data: FileItem, parent: Optional['Node'] = None):
        self.data = data
        self.children: List['Node'] = []
        self.parent = parent
Enter fullscreen mode Exit fullscreen mode

Why:

  • Clarity: Tree operations are explicit and understandable
  • CS Fundamentals: Standard tree algorithms (DFS, BFS) work naturally
  • Extensibility: Easy to add tree operations (find, count, height, etc.)
  • Debugging: Can inspect tree structure independently
  • Parent References: Node stores parent, enabling upward traversal

3. Text Format Choice

Tree-Formatted Text Over JSON/TOML/YAML (I may add them too in future versions)

project/
├── src/
│   └── main.py
└── tests/
Enter fullscreen mode Exit fullscreen mode

Why:

  • Copy-Paste Ready: Most documentation already uses this format
  • Visual Clarity: Hierarchy is immediately obvious
  • Source Matching: ChatGPT, Claude, docs all output trees
  • Natural Comments: ├── config.py # handles configuration
  • Zero Conversion: No need to translate from other formats Trade-off:
  • Parsing │ ├ └ ─ is harder than json.loads()

4. Zero Dependencies

  • Security: No third-party vulnerabilities
  • Compatibility: Works everywhere Python works
  • Installation Speed: No dependency resolution
  • Maintenance: No dependency updates to track
  • Size: Lightweight package

5. CLI + API Design

Provide Both Interfaces
CLI:

tree2fs tree.txt --verbose --dry-run
Enter fullscreen mode Exit fullscreen mode

Python API:

from tree2fs import create_from_tree

create_from_tree("tree.txt", verbose=True)
Enter fullscreen mode Exit fullscreen mode

Why Both:

  • CLI: Quick usage, shell scripts, CI/CD
  • API: Programmatic control, testing, integration
  • Different Users: Command-line users vs. Python developers ### 6. Dry-Run Mode Add Preview Without Creation
tree2fs tree.txt --dry-run
Enter fullscreen mode Exit fullscreen mode

Why:

  • Safety: Preview before making changes
  • Verification: Check if structure is correct
  • Testing: Verify parser without side effects
  • User Confidence: See what will happen

Architecture Diagram

┌─────────────────────────────────────────────────────────────┐
│                         tree2fs                             │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  ┌──────────┐     ┌──────────┐      ┌───────────────────┐   │
│  │   CLI    │────▶│   API    │────▶│  create_from_tree │   │
│  └──────────┘     └──────────┘      └─────────┬─────────┘   │
│                                               │             │
│  ┌────────────────────────────────────────────▼─────────┐   │
│  │                   TreeParser                         │   │
│  │  • parse_line(line) → FileItem                       │   │
│  │  • build_tree(file) → Node                           │   │
│  └────────────────────────────┬─────────────────────────┘   │
│                               │                             │
│                               ▼                             │
│  ┌─────────────────────────────────────────────────────┐    │
│  │              Tree Data Structure                    │    │
│  │                                                     │    │
│  │       Node                     FileItem             │    │
│  │    ┌──────────────┐         ┌──────────────┐        │    │
│  │    │ data         │◀───────│ filename     │        │    │
│  │    │ children[]   │         │ level        │        │    │
│  │    │ parent       │         │ comment      │        │    │
│  │    └──────────────┘         └──────────────┘        │    │
│  └────────────────────────────┬────────────────────────┘    │
│                               │                             │
│                               ▼                             │
│  ┌─────────────────────────────────────────────────────┐    │
│  │              FilesystemBuilder                      │    │
│  │  • build(root) → create structure                   │    │
│  │  • _traverse_and_create(node) → DFS                 │    │
│  │  • _create_directory(path)                          │    │
│  │  • _create_file(path)                               │    │
│  └─────────────────────────────┬───────────────────────┘    │
│                                │                            │
│                                ▼                            │
│  ┌─────────────────────────────────────────────────────┐    │
│  │                   Filesystem                        │    │
│  │            📁 Directories & 📄 Files               │    │
│  └─────────────────────────────────────────────────────┘    │
│                                                             │
└─────────────────────────────────────────────────────────────┘
Enter fullscreen mode Exit fullscreen mode

Try It Out

pip install tree2fs

GitHub: https://github.com/ABDELLAH-Hallou/tree2fs
Let me know what you think in the comments!

Top comments (0)