Skip to main content

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

Project description

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.

Contributing

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

License

This project is licensed under the MIT License.

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

build_filesystem_trie-0.1.0a5.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

build_filesystem_trie-0.1.0a5-py2.py3-none-any.whl (4.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file build_filesystem_trie-0.1.0a5.tar.gz.

File metadata

  • Download URL: build_filesystem_trie-0.1.0a5.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for build_filesystem_trie-0.1.0a5.tar.gz
Algorithm Hash digest
SHA256 8f37e86df71e2aadca4de71869d11b65766701bcb3ee6effad1e9b888e7600ae
MD5 19da88419778f712f7f53e55b158d518
BLAKE2b-256 055ffa5038532ba3a04f1f6f43f6f0920288c88bb492c8e1d5252fc249c4b38a

See more details on using hashes here.

File details

Details for the file build_filesystem_trie-0.1.0a5-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for build_filesystem_trie-0.1.0a5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 683716a7f95023d904d91b9dc2bc6a89facd41921c30d03bceeabea485b72ad2
MD5 ec0c2222dbbeac3772b6e3e9da81dacb
BLAKE2b-256 0c8250cc8ace60e02bd045a2abf016efed1afa2cbd914a42c32f73d83cbc61e7

See more details on using hashes here.

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