Skip to main content

pretty_print_directory: A Simple Utility to Pretty-Print Directory Contents

PyPI - Version python codecov tests code-quality license PRs contributors

1. Install

pip install pretty-print-directory

2. Usage

Basic usage

This package is largely used through the print_directory function, which takes a path and prints the contents of that directory in a tree-like format. It will print the contents of the directory and all subdirectories.

>>> from pretty_print_directory import print_directory
>>> from pathlib import Path
>>> import tempfile
>>> with tempfile.TemporaryDirectory() as tmpdir:
...     path = Path(tmpdir)
...     (path / "file1.txt").touch()
...     (path / "foo").mkdir()
...     (path / "bar").mkdir()
...     (path / "bar" / "baz.csv").touch()
...     print_directory(path)
├── bar
│   └── baz.csv
├── file1.txt
└── foo

You can change the print characters by passing a different config object to the print_directory function. This object is a PrintConfig dataclass:

>>> from pretty_print_directory import PrintConfig
>>> import inspect
>>> print(inspect.getsource(PrintConfig)) # doctest: +NORMALIZE_WHITESPACE
@dataclass
class PrintConfig:
    ...
    space: str = SPACE
    branch: str = BRANCH
    tee: str = TEE
    last: str = LAST
    file_extension: list[str] | None = None
    ...

Controlling what files are shown:

You can control what files are shown via the file_extension parameter of the PrintConfig object.

>>> with tempfile.TemporaryDirectory() as tmpdir:
...     path = Path(tmpdir)
...     (path / "file1.txt").touch()
...     (path / "foo").mkdir()
...     (path / "bar").mkdir()
...     (path / "bar" / "baz.csv").touch()
...     print_directory(path, config=PrintConfig(file_extension=".csv"))
└── bar
    └── baz.csv
>>> with tempfile.TemporaryDirectory() as tmpdir:
...     path = Path(tmpdir)
...     (path / "file1.txt").touch()
...     (path / "foo").mkdir()
...     (path / "bar").mkdir()
...     (path / "bar" / "baz.csv").touch()
...     print_directory(path, config=PrintConfig(file_extension=[".csv", ".txt"]))
├── bar
│   └── baz.csv
└── file1.txt

... or the ignore_regex parameter, which takes a regular expression to match file or directory names against (matching paths and any children of matching paths are not printed):

>>> with tempfile.TemporaryDirectory() as tmpdir:
...     path = Path(tmpdir)
...     (path / "file1.txt").touch()
...     (path / "bor.py").mkdir()
...     (path / "bar").mkdir()
...     (path / "bar" / "baz.csv").touch()
...     print_directory(path, config=PrintConfig(ignore_regex=r"b.r"))
└── file1.txt

Customizing the print style:

You can customize the print style with the space, branch, tee, and last parameters of the PrintConfig object.

>>> with tempfile.TemporaryDirectory() as tmpdir:
...     path = Path(tmpdir)
...     (path / "file1.txt").touch()
...     (path / "foo").mkdir()
...     (path / "bar").mkdir()
...     (path / "bar" / "baz.csv").touch()
...     print_directory(path, config=PrintConfig(tee="+-- "))
+-- bar
│   └── baz.csv
+-- file1.txt
└── foo

Customizing the print function:

You can also pass more keyword arguments to this function which will be passed to the underlying print call.

>>> from io import StringIO
>>> string_io = StringIO()
>>> with tempfile.TemporaryDirectory() as tmpdir:
...     path = Path(tmpdir)
...     (path / "file1.txt").touch()
...     (path / "foo").mkdir()
...     (path / "bar").mkdir()
...     (path / "bar" / "baz.csv").touch()
...     print_directory(path, file=string_io)
>>> print(string_io.getvalue())
├── bar
│   └── baz.csv
├── file1.txt
└── foo
<BLANKLINE>

As a pytest plugin

This package exports a pytest plugin that adds the print_directory function to the doctest namespace, so you can use it in doctests easily without importing it manually. It has no other special pytest functionality at this time.

3. Other notes

This repository may or may not be generally useful. There are a few other alternatives you should consider, including:

  1. https://github.com/chrizzFTD/printree
  2. https://github.com/itsbrex/print-pretty-tree
  3. https://github.com/AharonSambol/PrettyPrintTree
  4. https://github.com/Textualize/rich

Please feel free to file issues or submit PRs for features you want to add. I make no guarantees about long term maintenance, however.

4. Development

If you want to contribute, please fork the repo and make a PR. I will try to review it as soon as I can. Install the package locally with dev and test dependencies via:

pip install -e .[dev,test]

Then run the tests with:

pytest

Note that the code blocks in the README are tested via doctests. All tests in this repository are doctests.

Metadata

Release files for pretty-print-directory 0.1.3

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

Source distribution (sdist)

Source distribution for pretty-print-directory 0.1.3
File Size Uploaded Provenance
pretty_print_directory-0.1.3.tar.gz 13.6 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pretty-print-directory 0.1.3
File Interpreter ABI Platform
pretty_print_directory-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 21.9 kB

Release files / pretty_print_directory-0.1.3.tar.gz

Download URL pretty_print_directory-0.1.3.tar.gz
Size 13.6 kB
Tags Source
SHA-256 checksum
How to use checksums
08070aee8aee9101455ef8bacf2d540757a2ca84dae93f223d97defcdb6d3fff
BLAKE2b-256 checksum
How to use checksums
3b248c34b4ba69b9b5cf7dd1eb442bb98fb483441979ac2edd962fbc805a6553
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 9, 2025.

Transparency log

Release files / pretty_print_directory-0.1.3-py3-none-any.whl

Download URL pretty_print_directory-0.1.3-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e651113270a3db0dda524dd3f53dd2a1e98f7927c4c2fa0ddc73e8dc985dc8e6
BLAKE2b-256 checksum
How to use checksums
78eb6bad26f6d0a7fc6dc2aaa53caced96b46d3fd112686ccfa5559da6bb09da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 9, 2025.

Transparency log

Security for pretty-print-directory 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pretty_print_directory-0.1.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 9, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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