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_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.
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)
| File | Size | Uploaded | |
|---|---|---|---|
| build_filesystem_trie-0.1.0a6.tar.gz | 4.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| 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
|