-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
39 lines (31 loc) · 1.15 KB
/
Copy pathmain.py
File metadata and controls
39 lines (31 loc) · 1.15 KB
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
29
30
31
32
33
34
35
36
37
38
39
import sys
from pathlib import Path
DIR_SYMBOL = "📂 "
FILE_SYMBOL = "📄 "
INDENT = 3 * " "
def generate_tree(root_dir: Path, depth=0) -> str:
files_count = 0
tree_str = f"{INDENT * depth}{DIR_SYMBOL}{root_dir.name}\n"
for path in sorted(root_dir.iterdir()):
if path.is_dir():
subtree, subtree_files = generate_tree(path, depth + 1)
tree_str += subtree
files_count += subtree_files
else:
tree_str += f"{INDENT * (depth + 1)}{FILE_SYMBOL}{path.name}\n"
files_count += 1
return tree_str, files_count
def main(root_path):
print(f"Generating tree for {root_path}")
tree_str, files_num = generate_tree(root_path)
output_file = f"tree{files_num}.txt"
with open(output_file, "w", encoding="utf-8") as f:
f.write(tree_str)
print(f"Tree file Generated for {root_path}")
if __name__ == "__main__":
root_path = (sys.argv[1]) if len(sys.argv) > 1 else input("Enter the Path: ") or None
if root_path is not None:
root_path = root_path.strip('"')
main(Path(root_path))
else:
print("No path specified to generate.")