-
Notifications
You must be signed in to change notification settings - Fork 32
/
tree_generator.py
59 lines (38 loc) · 1.54 KB
/
tree_generator.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
def format_tree(tree, indent_char='| ', last_indent_char='|__ '):
lines = tree.strip().split('\n')
formatted_lines = []
min_indent = min(len(line) - len(line.strip()) for line in lines if line.strip())
for line in lines:
indent_level = (len(line) - len(line.strip()) - min_indent) // len(indent_char) + 1
formatted_line = line.strip()
if indent_level > 0:
formatted_line = indent_char * (indent_level - 1) + last_indent_char + formatted_line
formatted_lines.append(formatted_line)
formatted_tree = '\n'.join(formatted_lines)
return formatted_tree
def get_directory_tree(directory):
tree_structure = ""
for root, dirs, files in os.walk(directory):
current_level = root.count(os.sep) - directory.count(os.sep)
indent ="| " * current_level
tree_structure += f"{indent}{os.path.basename(root)}"
subindent = "| " * (current_level + 1)
for file in files:
tree_structure += f"{subindent}{file}\n"
return tree_structure
current_directory = os.getcwd()
tree_structure = get_directory_tree(current_directory)
formatted_tree = format_tree(tree_structure)
file_name = input("Enter the file name to save the tree. Make sure is ends in either .md or .txt: ")
with open(file_name, 'w') as f:
f.write(formatted_tree)
print(f"You did it man, '{file_name}' is ready to go in your directory.")
# src
# components
# About
# Nav
# assets
# images
# headshot.png
# index.js