-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirtree.py
28 lines (25 loc) · 1.06 KB
/
dirtree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from pathlib import Path
import os
# prefix components:
space = ' '
branch = '│ '
# pointers:
tee = '├── '
last = '└── '
ignore = {'.git', '__pycache__', '.vscode', '.idea', '.pytest_cache', '.github', 'venv', 'node_modules', "dist", "build"}
def tree(dir_path: Path, prefix: str=''):
"""A recursive generator, given a directory Path object
will yield a visual tree structure line by line
with each line prefixed by the same characters
"""
contents = list(dir_path.iterdir())
# contents each get pointers that are ├── with a final └── :
pointers = [tee] * (len(contents) - 1) + [last]
for pointer, path in zip(pointers, contents):
yield prefix + pointer + path.name
if path.is_dir(): # extend the prefix and recurse:
extension = branch if pointer == tee else space
# i.e. space because last, └── , above so no more |
yield from tree(path, prefix=prefix+extension)
if __name__ == '__main__':
print(*tree(Path(os.getcwd())), sep='\n')