scantree
Recursive directory iterator supporting:
- flexible filtering including wildcard path matching
- in memory representation of file-tree (for repeated access)
- efficient access to directory entry properties (
os.DirEntryinterface) extended with real path and path relative to the recursion root directory - detection and handling of cyclic symlinks
Installation
pip install scantree
Usage
See source code for full documentation, some generic examples below.
Get matching file paths:
from scantree import scantree, RecursionFilter
tree = scantree('/path/to/dir', RecursionFilter(match=['*.txt']))
print([path.relative for path in tree.filepaths()])
print([path.real for path in tree.filepaths()])
['d1/d2/file3.txt', 'd1/file2.txt', 'file1.txt']
['/path/to/other_dir/file3.txt', '/path/to/dir/d1/file2.txt', '/path/to/dir/file1.txt']
Access metadata of directory entries in file tree:
d2 = tree.directories[0].directories[0]
print(type(d2))
print(d2.path.absolute)
print(d2.path.real)
print(d2.path.is_symlink())
print(d2.files[0].relative)
scantree._node.DirNode
/path/to/dir/d1/d2
/path/to/other_dir
True
d1/d2/file3.txt
Aggregate information by operating on tree:
hello_count = tree.apply(
file_apply=lambda path: sum([
w.lower() == 'hello' for w in
path.as_pathlib().read_text().split()
]),
dir_apply=lambda dir_: sum(dir_.entries),
)
print(hello_count)
3
hello_count_tree = tree.apply(
file_apply=lambda path: {
'name': path.name,
'count': sum([
w.lower() == 'hello'
for w in path.as_pathlib().read_text().split()
])
},
dir_apply=lambda dir_: {
'name': dir_.path.name,
'count': sum(e['count'] for e in dir_.entries),
'sub_counts': [e for e in dir_.entries]
},
)
from pprint import pprint
pprint(hello_count_tree)
{'count': 3,
'name': 'dir',
'sub_counts': [{'count': 2, 'name': 'file1.txt'},
{'count': 1,
'name': 'd1',
'sub_counts': [{'count': 1, 'name': 'file2.txt'},
{'count': 0,
'name': 'd2',
'sub_counts': [{'count': 0,
'name': 'file3.txt'}]}]}]}
Flexible filtering:
without_hidden_files = scantree('.', RecursionFilter(match=['*', '!.*']))
without_palindrome_linked_dirs = scantree(
'.',
lambda paths: [
p for p in paths if not (
p.is_dir() and
p.is_symlink() and
p.name == p.name[::-1]
)
]
)
Comparison:
tree = scandir('path/to/dir')
# make some operations on filesystem, make sure file tree is the same:
assert tree == scandir('path/to/dir')
# tree contains absolute/real path info:
import shutil
shutil.copytree('path/to/dir', 'path/to/other_dir')
new_tree = scandir('path/to/other_dir')
assert tree != new_tree
assert (
[p.relative for p in tree.leafpaths()] ==
[p.relative for p in new_tree.leafpaths()]
)
Inspect symlinks:
from scantree import CyclicLinkedDir
file_links = []
dir_links = []
cyclic_links = []
def file_apply(path):
if path.is_symlink():
file_links.append(path)
def dir_apply(dir_node):
if dir_node.path.is_symlink():
dir_links.append(dir_node.path)
if isinstance(dir_node, CyclicLinkedDir):
cyclic_links.append((dir_node.path, dir_node.target_path))
scantree('.', file_apply=file_apply, dir_apply=dir_apply)
Metadata
Release files for scantree 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| scantree-0.0.4.tar.gz | 24.6 kB | Without provenance | Details for scantree-0.0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| scantree-0.0.4-py3-none-any.whl | Python 3 | none | any | Details for scantree-0.0.4-py3-none-any.whl |
Total release size: 45.3 kB
Release files / scantree-0.0.4.tar.gz
| Download URL | scantree-0.0.4.tar.gz |
|---|---|
| Size | 24.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
15bd5cb24483b04db2c70653604e8ea3522e98087db7e38ab8482f053984c0ac
|
|
BLAKE2b-256 checksum How to use checksums |
b3e440998faefc72ba1ddeb640a44fba92935353525dba110488806da8339c0b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.0 CPython/3.12.4
|
Release files / scantree-0.0.4-py3-none-any.whl
| Download URL | scantree-0.0.4-py3-none-any.whl |
|---|---|
| Size | 20.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7616ab65aa6b7f16fcf8e6fa1d9afaa99a27ab72bba05c61b691853b96763174
|
|
BLAKE2b-256 checksum How to use checksums |
93ce828467ddfa0d2fe473673026442d2032d552a168e42cfbf25fd0e5264e0c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.0 CPython/3.12.4
|
Security for scantree 0.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.