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_endflag:Trueif the node represents a file,Falsefor a directory. - Directory nodes have a
.childrendict 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
prefixwill contain the absolute path components of the directory containingtestdir.triewill be aTrieNodenamedtestdir, withis_end == Falseand childrenTrieNodes namedfile1.txtandsubdir, representing the full directory structure undertestdir.
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_componentswill contain the relative path components of a file or directory, relative to the directory containingtestdir.- It is possible to use
os.path.join(*prefix, *relative_path_components)to obtain the absolute path of the file or directory.
- It is possible to use
is_dirisFalsefor files andTruefor 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
Release history Release notifications | RSS feed
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
10c5ffde5ba79540689d6840933f2a7e374e2312084b2cf511bc8a7ab3e9d475
|
|
| MD5 |
91538995722ff328b9f2f18ce55ea7ce
|
|
| BLAKE2b-256 |
bdc324e0da8f1079f5ec7aa33c0e28fab3fa143e25efeb76258097eed3a14a27
|
File details
Details for the file build_filesystem_trie-0.1.0a6-py2.py3-none-any.whl.
File metadata
- Download URL: build_filesystem_trie-0.1.0a6-py2.py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
21a30ea166f36df4ca208fcb4a8efd5b939fc82d1b2f61b43dc5d8d4db7b93b8
|
|
| MD5 |
92847c6606090606365f0b7510a3ee95
|
|
| BLAKE2b-256 |
5821e0a4534beb8ecc475c3e7e2c77336e8fe9f17d0e6bb946fdf5343e0fb3b3
|