-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirtree.py
133 lines (111 loc) · 5.26 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
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os
import argparse
def simple_tree(dir_path):
print(os.path.basename(dir_path) + '/')
for item in os.listdir(dir_path):
item_path = os.path.join(dir_path, item)
if os.path.isdir(item_path):
simple_tree(item_path)
else:
print(item)
# Platform-Independent Path Handling for Absolute Path Tree
def absolute_path_tree_pi(dir_path):
print(dir_path)
for item in os.listdir(dir_path):
item_path = os.path.join(dir_path, item)
if os.path.isdir(item_path):
absolute_path_tree_pi(item_path)
else:
print(item_path)
# User-Defined Path Formatting for Absolute Path Tree
def absolute_path_tree(dir_path, path_style='auto'):
if path_style == 'auto':
path_style = os.path.sep # Use the default separator based on the platform
print(dir_path.replace(os.path.sep, path_style))
for item in os.listdir(dir_path):
item_path = os.path.normpath(os.path.join(dir_path, item))
if os.path.isdir(item_path):
absolute_path_tree(item_path, path_style)
else:
print(item_path.replace(os.path.sep, path_style))
# Platform-Independent Path Handling for Relative Path Tree
def relative_path_tree_pi(dir_path, relative_path=""):
current_path = os.path.join(relative_path, os.path.basename(dir_path))
print(current_path)
for item in os.listdir(dir_path):
item_path = os.path.join(dir_path, item)
if os.path.isdir(item_path):
relative_path_tree_pi(item_path, current_path)
else:
print(os.path.join(current_path, item))
# User-Defined Path Formatting for Relative Path Tree
def relative_path_tree(dir_path, relative_path="", path_style='auto'):
if path_style == 'auto':
path_style = os.path.sep # Use the default separator based on the platform
current_path = os.path.join(relative_path, os.path.basename(dir_path))
print(current_path.replace(os.path.sep, path_style) + path_style)
for item in os.listdir(dir_path):
item_path = os.path.join(dir_path, item)
if os.path.isdir(item_path):
relative_path_tree(item_path, current_path, path_style) # Use current_path instead of os.path.join(current_path, item)
else:
print(os.path.join(current_path, item).replace(os.path.sep, path_style))
def directory_tree_structure(dir_path, indent=''):
print(indent[:-1] + '+-- ' + os.path.basename(dir_path) + '/')
indent += ' '
for item in os.listdir(dir_path):
item_path = os.path.join(dir_path, item)
if os.path.isdir(item_path):
directory_tree_structure(item_path, indent)
else:
print(indent + '+-- ' + item)
def beautify_tree_structure(dir_path, indent=''):
print(f"{indent}- {os.path.basename(dir_path)}")
for item in os.listdir(dir_path):
item_path = os.path.join(dir_path, item)
if os.path.isdir(item_path):
beautify_tree_structure(item_path, indent + ' |')
else:
print(f"{indent} |- {item}")
def main():
parser = argparse.ArgumentParser(description='Directory Tree Visualizer')
# Specify the function to execute (simple, abs_pi, rel_pi, abs, rel, tree, beautify)
parser.add_argument('function', choices=['simple', 'abs_pi', 'rel_pi', 'abs', 'rel', 'tree', 'beautify'],
help='Function to execute. Choose one of the following: simple, abs_pi, rel_pi, abs, rel, tree, beautify.')
# Specify the path parameter
parser.add_argument('path', help='Path parameter. Enter the path to the target directory.')
# Specify an optional argument for path style formatting
parser.add_argument('--path_style', default='auto',
help='Path style for formatting. Choose one of the following: auto, /, \\ (default: auto).\n'
'Examples:\n'
' - For Unix-like paths: --path_style /\n'
' - For Windows paths: --path_style \\\n'
' - Automatic detection based on the platform: --path_style auto')
args = parser.parse_args()
if not os.path.exists(args.path):
print("Error: The specified path does not exist.")
else:
base_path = os.path.abspath(args.path)
if args.function == 'simple':
# Simple tree
simple_tree(base_path)
elif args.function == 'abs_pi':
# Platform-Independent Path Handling for absolute path tree
absolute_path_tree_pi(base_path)
elif args.function == 'rel_pi':
# Platform-Independent Path Handling for relative path tree
relative_path_tree_pi(base_path)
elif args.function == 'abs':
# User-Defined Path Formatting for absolute path tree
absolute_path_tree(base_path, args.path_style)
elif args.function == 'rel':
# User-Defined Path Formatting for relative path tree
relative_path_tree(base_path, path_style=args.path_style)
elif args.function == 'tree':
# Directory Tree Structure
directory_tree_structure(base_path)
elif args.function == 'beautify':
# Beautify Tree Structure
beautify_tree_structure(base_path)
if __name__ == '__main__':
main()