Skip to main content

A Python utility that functions as the tree Linux command with cross-platform support

Project description

nice tree

alt text

A robust Python utility that functions like the Unix tree command with cross-platform support (macOS, Linux, and Windows). Display directory hierarchies in a beautiful, formatted structure with extensive customization options.

Python Version License

Features

  • Cross-platform support: Works seamlessly on macOS, Linux, and Windows
  • Beautiful output: Unicode tree drawing with optional colors
  • Customizable: Control depth, filtering, and output format
  • Performance: Efficient directory traversal
  • Symbolic link handling: Properly handles symlinks with circular reference detection
  • File sizes: Display file/directory sizes in human-readable format
  • Multiple formats: Tree, JSON, and simple text output
  • Hidden file control: Show or hide dot-files
  • Pattern matching: Ignore files matching patterns (wildcards supported)
  • Statistics: Display file/directory counts

Installation

From PyPI (Recommended)

pip install nice-tree

From source

git clone https://github.com/teatonedev/nicetree.git
cd nicetree
pip install -e .

Development installation

git clone https://github.com/teatonedev/nicetree.git
cd nicetree
pip install -e ".[dev]"

Quick Start

Command Line Usage

# Display current directory tree
nicetree

# Display specific directory
nicetree /path/to/directory

# Limit depth to 2 levels
nicetree --depth 2

# Show file sizes
nicetree --size

# Ignore Python cache files
nicetree --ignore "*.pyc" --ignore "__pycache__"

# Show hidden files
nicetree --all

# Show statistics
nicetree --statistics

# Follow symbolic links
nicetree --follow

# Use ASCII characters
nicetree --charset ascii

# Disable colors
nicetree --no-colors

# JSON output
nicetree --format json

Python API Usage

from nicetree import TreeGenerator, TreeFormatter, OutputFormat

# Create generator
generator = TreeGenerator(
    root_path="/path/to/dir",
    max_depth=3,
    show_hidden=False,
    ignore_patterns=["*.pyc", "__pycache__"],
    show_size=True,
)

# Generate tree
root_node = generator.generate()

# Create formatter
formatter = TreeFormatter(
    charset="unicode",
    colors=True,
    show_size=True,
    format_type=OutputFormat.TREE,
)

# Display tree
formatter.print_tree(root_node)

# Or get as string
output = formatter.format_tree(root_node)
print(output)

# Get statistics
stats = generator.get_tree_stats(root_node)
print(f"Files: {stats['file_count']}, Directories: {stats['dir_count']}")

CLI Reference

Options

Option Short Type Description
--depth -d INT Limit tree depth to N levels
--ignore -i PATTERN Ignore files matching PATTERN (can be used multiple times)
--all -a - Show hidden files (starting with .)
--follow -L - Follow symbolic links
--size -s - Show file sizes
--statistics -S - Show statistics (file/directory counts)
--charset - {auto,unicode,ascii} Character set for drawing (default: auto)
--no-colors - - Disable colored output
--format -f {tree,json,simple} Output format (default: tree)
--version -V - Show version information

Examples

# Display current directory with all options
nicetree . --all --depth 3 --size --statistics

# Export as JSON
nicetree /var/log --format json > tree.json

# Show only Python files (2 levels deep)
nicetree . --depth 2 --ignore "*.pyc" --ignore "*.pyo"

# ASCII output for piping to other commands
nicetree --charset ascii --no-colors

# Show large directories
nicetree /home --depth 2 --size --statistics

API Reference

TreeGenerator

Main class for generating directory tree structures.

Constructor

TreeGenerator(
    root_path: str = ".",
    max_depth: Optional[int] = None,
    ignore_patterns: Optional[List[str]] = None,
    show_hidden: bool = False,
    follow_symlinks: bool = False,
    show_size: bool = False,
    case_sensitive: bool = True,
)

Parameters

  • root_path: Starting directory (default: current directory)
  • max_depth: Maximum traversal depth (None = unlimited)
  • ignore_patterns: List of wildcard patterns to ignore
  • show_hidden: Include hidden files (starting with .)
  • follow_symlinks: Follow symbolic links (with circular detection)
  • show_size: Calculate and display file/directory sizes
  • case_sensitive: Use case-sensitive pattern matching

Methods

  • generate() -> Optional[TreeNode]: Build and return the tree structure
  • get_tree_stats(node: Optional[TreeNode]) -> Dict[str, int]: Get statistics about the tree

TreeFormatter

Formats and displays tree structures.

Constructor

TreeFormatter(
    charset: str = "auto",
    colors: bool = True,
    show_size: bool = False,
    format_type: OutputFormat = OutputFormat.TREE,
)

Parameters

  • charset: "unicode", "ascii", or "auto" (auto-detect)
  • colors: Use ANSI colors in output
  • show_size: Display file sizes
  • format_type: OutputFormat enum (TREE, JSON, or SIMPLE)

Methods

  • format_tree(node: Optional[TreeNode], show_root: bool = True) -> str: Return formatted tree as string
  • print_tree(node: Optional[TreeNode], show_root: bool = True) -> None: Print tree to stdout

TreeNode

Represents a single node in the directory tree.

Attributes

  • name: Node name (filename or directory name)
  • path: Full Path object
  • is_dir: Boolean indicating if node is a directory
  • is_symlink: Boolean indicating if node is a symbolic link
  • children: List of child TreeNode objects
  • size: File/directory size in bytes

Platform-Specific Notes

macOS

  • Full Unicode support with automatic color detection
  • Symbolic links fully supported
  • All features available

Linux

  • Full Unicode support
  • Color support depends on TERM variable
  • Excellent performance on large directory trees
  • All features available

Windows

  • ASCII character set used by default (can override)
  • Color support in Windows 10+ with ANSICON
  • Path handling automatically converts to Windows format
  • Symbolic link support available with appropriate permissions

Performance

nicetree is optimized for performance:

  • Lazy loading: Only traverses necessary paths based on options
  • Efficient symlink detection: Tracks visited inodes to prevent loops
  • Streaming output: Outputs as it traverses (for large trees)
  • Memory efficient: Uses generators where possible

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.

Author

Teatonedev Batt (abbilginn@hotmail.com)

Changelog

Version 1.0.0 (2026-01-26)

  • Initial release
  • Full cross-platform support
  • CLI and Python API
  • Comprehensive testing
  • PyPI distribution ready

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nice_tree-1.0.0.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nice_tree-1.0.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file nice_tree-1.0.0.tar.gz.

File metadata

  • Download URL: nice_tree-1.0.0.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for nice_tree-1.0.0.tar.gz
Algorithm Hash digest
SHA256 2da3cc2b1053d436fab2620f2a6f79b3fb00690d09cf57b67792f21423a08849
MD5 07dae38e4fd44414e2821899f107d319
BLAKE2b-256 43f6ee8a93415fee9fe3bb4aaf9e2be8dc1dd2edb240db136e0c50d2757d35fe

See more details on using hashes here.

File details

Details for the file nice_tree-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: nice_tree-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for nice_tree-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b87236ccdacf74afc34e23ab4e5ef4f7d92bf76d82f2db3164d276d690c18ace
MD5 4ec62d1b19a095616e8114b4a5b31c5f
BLAKE2b-256 dc174189bbb8fa430daf97a46abf17e621f72c218d46fb2c8ed5d0c7d1381c8f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page