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
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:
- https://github.com/chrizzFTD/printree
- https://github.com/itsbrex/print-pretty-tree
- https://github.com/AharonSambol/PrettyPrintTree
- 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
08070aee8aee9101455ef8bacf2d540757a2ca84dae93f223d97defcdb6d3fff
|
|
| MD5 |
874c78f50d80e9508677f9f646834d19
|
|
| BLAKE2b-256 |
3b248c34b4ba69b9b5cf7dd1eb442bb98fb483441979ac2edd962fbc805a6553
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pretty_print_directory-0.1.3.tar.gz -
Subject digest:
08070aee8aee9101455ef8bacf2d540757a2ca84dae93f223d97defcdb6d3fff - Sigstore transparency entry: 268807901
- Sigstore integration time:
-
Permalink:
mmcdermott/pretty-print-directory@607ce1a66755fbc9ffc0d8c2d47db41211d11050 -
Branch / Tag:
refs/tags/0.1.3 - Owner: https://github.com/mmcdermott
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-build.yaml@607ce1a66755fbc9ffc0d8c2d47db41211d11050 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pretty_print_directory-0.1.3-py3-none-any.whl.
File metadata
- Download URL: pretty_print_directory-0.1.3-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e651113270a3db0dda524dd3f53dd2a1e98f7927c4c2fa0ddc73e8dc985dc8e6
|
|
| MD5 |
885cb02a3c09759e123de5de50d381f6
|
|
| BLAKE2b-256 |
78eb6bad26f6d0a7fc6dc2aaa53caced96b46d3fd112686ccfa5559da6bb09da
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pretty_print_directory-0.1.3-py3-none-any.whl -
Subject digest:
e651113270a3db0dda524dd3f53dd2a1e98f7927c4c2fa0ddc73e8dc985dc8e6 - Sigstore transparency entry: 268807908
- Sigstore integration time:
-
Permalink:
mmcdermott/pretty-print-directory@607ce1a66755fbc9ffc0d8c2d47db41211d11050 -
Branch / Tag:
refs/tags/0.1.3 - Owner: https://github.com/mmcdermott
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-build.yaml@607ce1a66755fbc9ffc0d8c2d47db41211d11050 -
Trigger Event:
push
-
Statement type: