Skip to main content

A simple utility to pretty-print a directory tree, suitable for use in pytest test cases.

Project description

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.

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

pretty_print_directory-0.1.3.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

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

pretty_print_directory-0.1.3-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file pretty_print_directory-0.1.3.tar.gz.

File metadata

  • Download URL: pretty_print_directory-0.1.3.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pretty_print_directory-0.1.3.tar.gz
Algorithm Hash digest
SHA256 08070aee8aee9101455ef8bacf2d540757a2ca84dae93f223d97defcdb6d3fff
MD5 874c78f50d80e9508677f9f646834d19
BLAKE2b-256 3b248c34b4ba69b9b5cf7dd1eb442bb98fb483441979ac2edd962fbc805a6553

See more details on using hashes here.

Provenance

The following attestation bundles were made for pretty_print_directory-0.1.3.tar.gz:

Publisher: python-build.yaml on mmcdermott/pretty-print-directory

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pretty_print_directory-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for pretty_print_directory-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e651113270a3db0dda524dd3f53dd2a1e98f7927c4c2fa0ddc73e8dc985dc8e6
MD5 885cb02a3c09759e123de5de50d381f6
BLAKE2b-256 78eb6bad26f6d0a7fc6dc2aaa53caced96b46d3fd112686ccfa5559da6bb09da

See more details on using hashes here.

Provenance

The following attestation bundles were made for pretty_print_directory-0.1.3-py3-none-any.whl:

Publisher: python-build.yaml on mmcdermott/pretty-print-directory

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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