Initializing
Back to Projects
Year2024
DomainFullstack
AccessOpen Source
Complexity0 / 10
PythonCLIFile System
FullstackArchived

Tree Viewer

An interactive CLI tool that displays directory structures with customizable depth, hidden file filtering, and beautiful tree visualization.

# Tree Viewer

An interactive CLI tool that displays directory structures with beautiful tree visualization. No command-line arguments required — just run and answer prompts.

Features

FeatureDescription
InteractiveNo CLI args needed, prompts guide you
Customizable DepthSet how many levels deep to display
Hidden FilesToggle visibility with Yes/No
Emoji TreeVisual folder structure with icons
Standalone .exePyInstaller packaging supported

Usage

bash
python tree_viewer.py

Interactive Prompts

code
--- Select Root Directory ---
Enter the path to the folder you want to display: C:\MyProject

--- Tree Depth ---
How many levels deep should the tree go? (1-10): 3

--- Hidden Files ---
Show hidden files? (Y/N): Y

📂 MyProject/
├── 📁 src/
│   ├── 📄 main.py
│   └── 📁 utils/
│       └── 📄 helpers.py
├── 📁 tests/
│   └── 📄 test_main.py
└── 📄 README.md

Core Implementation

python
import os
from pathlib import Path

def display_tree(root_path: str, max_depth: int, show_hidden: bool):
    root = Path(root_path)
    
    def walk_tree(path: Path, prefix: str = "", depth: int = 0):
        if depth > max_depth:
            return
        
        # Get entries
        try:
            entries = sorted(path.iterdir(), key=lambda x: (not x.is_dir(), x.name))
        except PermissionError:
            return
        
        for i, entry in enumerate(entries):
            # Skip hidden files if not requested
            if not show_hidden and entry.name.startswith('.'):
                continue
                
            is_last = i == len(entries) - 1
            connector = "└── " if is_last else "├── "
            
            # Emoji based on type
            icon = "📁" if entry.is_dir() else "📄"
            print(f"{prefix}{connector}{icon} {entry.name}")
            
            # Recurse for directories
            if entry.is_dir():
                new_prefix = prefix + ("    " if is_last else "")
                walk_tree(entry, new_prefix, depth + 1)
    
    print(f"📂 {root.name}/")
    walk_tree(root, "", 1)

PyInstaller Packaging

bash
pyinstaller --onefile --name "TreeViewer" tree_viewer.py

Output: dist/TreeViewer.exe — runs on any Windows machine without Python.

Use Cases

  1. Project overview: Visualize folder structure for documentation
  2. Code reviews: Understand project layout quickly
  3. File organization: Debug directory structure issues
  4. Sharing: Export tree view for team communication

Architecture Feedback

Spotted a potential optimization or antipattern? Let me know.

Submit a Technical Suggestion