Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

build-filesystem-trie

Programmatically generate a trie representing a section of the filesystem, closely matching what the UNIX tree command outputs.

For both file or directory paths, absolute or relative, the function returns a tuple containing:

  • The absolute path components of the directory containing the file or directory.
  • The trie rooted at the specified path, whether that path points to a file or a directory.

Trie Node Semantics:

  • All nodes have a value (the file or directory name).
  • All nodes have an is_end flag: True if the node represents a file, False for a directory.
  • Directory nodes have a .children dict mapping containing file and directory names to child nodes.

Installation

pip install build-filesystem-trie

Example

Suppose the file layout is:

- testdir/
  - file1.txt
  - subdir/
    - file2.txt

Running

from build_filesystem_trie import build_filesystem_trie, iterate_relative_path_components_is_dir_tuples

prefix, trie = build_filesystem_trie('testdir')

for relative_path_components, is_dir in iterate_relative_path_components_is_dir_tuples(trie):
    print(
        '%s- %s%s' % (
            '  ' * (len(relative_path_components) - 1),
            relative_path_components[-1],
            '/' if is_dir else '',
        )
    )

prints:

- testdir/
  - file1.txt
  - subdir/
    - file2.txt
  • prefix will contain the absolute path components of the directory containing testdir.
  • trie will be a TrieNode named testdir, with is_end == False and children TrieNodes named file1.txt and subdir, representing the full directory structure under testdir.

By default, skips dotted (hidden) files and directories in a directory.

To recurse dotted (hidden) files and directories in a directory, pass the argument recurse_dotted=True

  • relative_path_components will contain the relative path components of a file or directory, relative to the directory containing testdir.
    • It is possible to use os.path.join(*prefix, *relative_path_components) to obtain the absolute path of the file or directory.
  • is_dir is False for files and True for directories.

This is analogous to the output of the UNIX tree command.

Command-line Usage

You can also print a tree from the command line:

python -m build_filesystem_trie path/to/your/file_or_directory

By default, it skips dotted (hidden) files and directories.
To include hidden files/directories, use:

python -m build_filesystem_trie path/to/dir --recurse-dotted

Example output:

- testdir/
  - file1.txt
  - subdir/
    - file2.txt

To generate a YAML filesystem tree, use:

python -m build_filesystem_trie path/to/dir --yaml

Example output:

- "testdir":
  - "file1.txt"
  - "subdir":
    - "file2.txt"

Contributing

Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.

License

This project is licensed under the MIT License.

Release files for build-filesystem-trie 0.1.0a6

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

Source distribution (sdist)

Source distribution for build-filesystem-trie 0.1.0a6
File Size Uploaded
build_filesystem_trie-0.1.0a6.tar.gz 4.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for build-filesystem-trie 0.1.0a6
File Interpreter ABI Platform
build_filesystem_trie-0.1.0a6-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 9.9 kB

Release files / build_filesystem_trie-0.1.0a6.tar.gz

Download URL build_filesystem_trie-0.1.0a6.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
10c5ffde5ba79540689d6840933f2a7e374e2312084b2cf511bc8a7ab3e9d475
BLAKE2b-256 checksum
How to use checksums
bdc324e0da8f1079f5ec7aa33c0e28fab3fa143e25efeb76258097eed3a14a27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release files / build_filesystem_trie-0.1.0a6-py2.py3-none-any.whl

Download URL build_filesystem_trie-0.1.0a6-py2.py3-none-any.whl
Size 5.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
21a30ea166f36df4ca208fcb4a8efd5b939fc82d1b2f61b43dc5d8d4db7b93b8
BLAKE2b-256 checksum
How to use checksums
5821e0a4534beb8ecc475c3e7e2c77336e8fe9f17d0e6bb946fdf5343e0fb3b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release history Release notifications | RSS feed

This release

0.1.0a6 This release

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