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.2 (2026-01-26)

  • Initial release
  • Full cross-platform support
  • CLI and Python API
  • Comprehensive testing
  • Logo is added.

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.2.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.2-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nice_tree-1.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 36fa9a10f3f64104d158f5f614867bd6211b34bf0b68cb3f3a18ba96aabb4cdb
MD5 7bf960a257eb5277a6b1f2a7ded690f1
BLAKE2b-256 9c931ff9ff8afd2e38ce320110668ccf7f4de99338e81b6a507349c24def8e40

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nice_tree-1.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3d46f201f4b4ec20932f9ca1e636594579d557c00dd44daad0c4ea48b3559584
MD5 e59c3c09c02a63bbc50cb0b9b764181b
BLAKE2b-256 e834584a33feaf516e14071e0f42ef139c987b65c3968763ee12117b349d1c7d

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