Skip to main content

pytree

A Python CLI utility for visualizing folder trees with sizes and counts.

Installation

pytree requires Python version 3.10+ in order to run. You can install pytree in your Python environment with the command:

pip install pytree2

Usage

pytree [-h] [-d] [-s] [-c] [-x EXTENSION] [-k KEYWORD] [-l LEVEL] [-loc] [-o OUTPUT_PATH] [-q] [start_path ...]
pytree - a python cli utility for visualizing folder trees with sizes and counts

positional arguments:
  start_path            defines path to directory to start building the tree

options:
  -h, --help            show this help message and exit
  -d, --dirs-only       tree displays directories only, and does not show files inside folders
  -s, --show-sizes      tree displays files and folder sizes, in mega or gigabytes
  -c, --show-counts     tree displays the number of files or folders inside each directory
  -x EXTENSION, --extension EXTENSION
                        tree will include only files that match given extension (e.g. ".txt", ".pdf")
  -k KEYWORD, --keyword KEYWORD
                        tree will include only files that contain specific keyword on file name
  -l LEVEL, --level LEVEL
                        defines tree's depth (until which subfolder tree will be created) [0=start_path, -1=all]
  -loc, --lines-of-code
                        tree displays the number of lines of code/comment for .py files in dir
  -o OUTPUT_PATH, --output-path OUTPUT_PATH
                        saves tree as a table in given output path [.csv]
  -q, --quiet           only saves tree to output path (does not print tree on terminal)

Examples

Basic usage

pytree test_folder
test_folder
├── another_folder
│   ├── empty_folder
│   └── one_mb_file.txt
└── folder
    ├── a_python_file.py
    ├── folder_inside_folder
    │   ├── not_a_text_file.pdf
    │   ├── ten_kb_file.txt
    │   └── two_mb_file.txt
    └── ten_mb_file.txt

Using optional arguments

By concatenating the optional arguments, you can get a clear view of the folder structure. Additionally, pytree will print a summary line in the end, with the folder/file count and total size.

pytree test_folder -dcs
test_folder [2] (13 mb)
├── another_folder [2] (1 mb)
│   └── empty_folder [0] (0 bytes)
└── folder [3] (12 mb)
    └── folder_inside_folder [3] (2 mb)

5 folders, 6 files, 13 mb

Specifying extension/keyword

You can also specify a search keyword (by passing -x your_extension) or keyword (by passing -k your_keyword), e.g:

pytree test_folder -cs -x .pdf
test_folder [2] (136 bytes)
├── another_folder [1] (0 bytes)
│   └── empty_folder [0] (0 bytes)
└── folder [1] (136 bytes)
    └── folder_inside_folder [1] (136 bytes)
        └── not_a_text_file.pdf (136 bytes)

5 folders, 6 files (1 valid), 136 bytes

Notice that by using this option together with the -c and -s flags, the counts and sizes in the final summary line will contain a counter for files matching search criteria, and the total size will reflect only matching files, providing an easy and quick way of scanning folders and identifying large files of a specified extension/keyword.

Saving tree

You can save the tree (file and folder information) by specifying an output path (by passing -o path/to/table.csv), e.g:

pytree test_folder -cs -o tree.csv
test_folder [2] (13 mb)
├── another_folder [2] (1 mb)
│   ├── empty_folder [0] (0 bytes)
│   └── one_mb_file.txt (1 mb)
└── folder [3] (12 mb)
    ├── a_python_file.py (154 bytes)
    ├── folder_inside_folder [4] (2 mb)
    │   ├── another_python_file.py (314 bytes)
    │   ├── not_a_text_file.pdf (136 bytes)
    │   ├── ten_kb_file.txt (10 kb)
    │   └── two_mb_file.txt (2 mb)
    └── ten_mb_file.txt (10 mb)

5 folders, 7 files, 13 mb
Saved output table to "tree.csv"

The output .csv will contain the same information as specified by the keywords, such as level (-l), keyword (-k), size (-s) and dirs_only (-d).

Tip: It can be useful to couple the --quiet flag when saving output, especially when working with thousands of files (prevents attempt to print the whole tree on terminal).

Lines of code

pytree can also be used to obtain the count (and percentage) of lines of code (loc) and comments for python files inside input directory, e.g:

pytree test_folder -c -loc
test_folder [2]
├── another_folder [1]
│   └── empty_folder [0]
└── folder [2]
    ├── a_python_file.py {2 lines of code (33%), 4 comments (67%)}
    └── folder_inside_folder [1]
        └── another_python_file.py {4 lines of code (40%), 6 comments (60%)}

5 folders, 7 files (2 valid), 6 lines of code (38%), 10 comments (62%)

This will provide a lines of code and comments count for all .py files in the tree, as well as a summary for the whole directory.

Metadata

Release files for pytree2 0.5.6

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

Source distribution (sdist)

Source distribution for pytree2 0.5.6
File Size Uploaded
pytree2-0.5.6.tar.gz 23.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytree2 0.5.6
File Interpreter ABI Platform
pytree2-0.5.6-py3-none-any.whl Python 3 none any Details

Total release size: 50.1 kB

Release files / pytree2-0.5.6.tar.gz

Download URL pytree2-0.5.6.tar.gz
Size 23.9 kB
Tags Source
SHA-256 checksum
How to use checksums
343dbdf8e6069f56ea243fa019700aac68683e697f01b35738f2db93f766a82f
BLAKE2b-256 checksum
How to use checksums
0e1a6a838f842d76e65cbbae4582eb9be7d0aefd59bb19dfcf97ed584631f758
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.21

Release files / pytree2-0.5.6-py3-none-any.whl

Download URL pytree2-0.5.6-py3-none-any.whl
Size 26.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c646a45a0bd95916fb4666a8e8c234759e000f1ed92eb4bf444d717a1dac9a8d
BLAKE2b-256 checksum
How to use checksums
c030a22c288ef04aac771496a4db8a452cda89c05a15d59b168ede8ab6281fd4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.21

Release history Release notifications | RSS feed

This release

0.5.6 This release

2 release files

0.5.5

2 release files

0.5.4

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

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