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.

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.

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.0a6.tar.gz (4.7 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.0a6-py2.py3-none-any.whl (5.3 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: build_filesystem_trie-0.1.0a6.tar.gz
  • Upload date:
  • Size: 4.7 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.0a6.tar.gz
Algorithm Hash digest
SHA256 10c5ffde5ba79540689d6840933f2a7e374e2312084b2cf511bc8a7ab3e9d475
MD5 91538995722ff328b9f2f18ce55ea7ce
BLAKE2b-256 bdc324e0da8f1079f5ec7aa33c0e28fab3fa143e25efeb76258097eed3a14a27

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for build_filesystem_trie-0.1.0a6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 21a30ea166f36df4ca208fcb4a8efd5b939fc82d1b2f61b43dc5d8d4db7b93b8
MD5 92847c6606090606365f0b7510a3ee95
BLAKE2b-256 5821e0a4534beb8ecc475c3e7e2c77336e8fe9f17d0e6bb946fdf5343e0fb3b3

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