Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 933207f

Browse files
Directory Tree Visualizer script is added
1 parent 4504e54 commit 933207f

File tree

2 files changed

+312
-0
lines changed

2 files changed

+312
-0
lines changed

‎Directory Tree Visualizer/README.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Directory Tree Visualizer (dirtree.py)
2+
3+
dirtree.py is a Python utility toolkit designed for effective directory navigation and visualization. It offers various functions to suit different visualization needs, allowing users to explore and understand project hierarchies. Whether you need a simple tree view or a beautified structure, dirtree.py has you covered.
4+
5+
## Main Features
6+
7+
- **Simple Tree:** Display a simple tree view of the directory.
8+
- **Absolute Path Tree (Platform-Independent):** Display the absolute path tree using forward slashes or backward slashes, automatically adjusting to the underlying operating system.
9+
- **Relative Path Tree (Platform-Independent):** Display the relative path tree using forward slashes or backward slashes, automatically adjusting to the underlying operating system.
10+
- **Absolute Path Tree (User-Defined Style):** Display the absolute path tree with a user-defined path style (Unix or Windows).
11+
- **Relative Path Tree (User-Defined Style):** Display the relative path tree with a user-defined path style (Unix or Windows).
12+
- **Directory Tree Structure:** Display a structured tree view of the directory.
13+
- **Beautify Tree Structure:** Display a beautified tree view of the directory.
14+
15+
## Prerequisites and Dependencies
16+
17+
- Python 3.x
18+
19+
## Usage
20+
21+
### Simple Tree:
22+
23+
Display a simple tree view of the directory.
24+
25+
```bash
26+
python dirtree.py simple /path/to/directory
27+
```
28+
29+
**Example Output:**
30+
31+
```bash
32+
directory/
33+
file1.txt
34+
file2.txt
35+
subdirectory/
36+
file3.txt
37+
```
38+
39+
### Absolute Path Tree (Platform-Independent):
40+
41+
Display the absolute path tree using forward slashes or backward slashes depending upon which operating system are you using. This script automatically adjusts to the underlying operating system, using forward slashes for Unix-like systems and backward slashes for Windows systems.
42+
43+
```bash
44+
python dirtree.py abs_pi /path/to/directory
45+
```
46+
47+
**Example Output for Windows:**
48+
49+
```bash
50+
E:\path\to\directory
51+
E:\path\to\directory\file1.txt
52+
E:\path\to\directory\file2.txt
53+
E:\path\to\directory\subdirectory
54+
E:\path\to\directory\subdirectory\file3.txt
55+
```
56+
57+
**Example Output for Linux:**
58+
59+
```bash
60+
E:/path/to/directory
61+
E:/path/to/directory/file1.txt
62+
E:/path/to/directory/file2.txt
63+
E:/path/to/directory/subdirectory
64+
E:/path/to/directory/subdirectory/file3.txt
65+
```
66+
67+
### Relative Path Tree (Platform-Independent):
68+
69+
Display the relative path tree using forward slashes or backward slashes depending upon which operating system are you using. This script automatically adjusts to the underlying operating system, using forward slashes for Unix-like systems and backward slashes for Windows systems.
70+
71+
```bash
72+
python dirtree.py rel_pi /path/to/directory
73+
```
74+
75+
**Example Output for Windows:**
76+
77+
```bash
78+
directory
79+
directory\file1.txt
80+
directory\file2.txt
81+
directory\subdirectory
82+
directory\subdirectory\file3.txt
83+
```
84+
85+
**Example Output for Linux:**
86+
87+
```bash
88+
directory
89+
directory/file1.txt
90+
directory/file2.txt
91+
directory/subdirectory
92+
directory/subdirectory/file3.txt
93+
```
94+
95+
### Absolute Path Tree (User-Defined Style):
96+
97+
Display the absolute path tree with a user-defined path style. This script enables users to explicitly specify the desired path style (Unix or Windows), offering flexibility and consistency across different operating systems.
98+
99+
Change the --path_style parameter as needed, '/' for unix like path formatting and '\' for windows like path format.
100+
101+
```bash
102+
python dirtree.py abs /path/to/directory --path_style /
103+
```
104+
105+
**Example Output:**
106+
107+
```bash
108+
E:/path/to/directory
109+
E:/path/to/directory/file1.txt
110+
E:/path/to/directory/file2.txt
111+
E:/path/to/directory/subdirectory
112+
E:/path/to/directory/subdirectory/file3.txt
113+
```
114+
115+
### Relative Path Tree (User-Defined Style):
116+
117+
Display the absolute path tree with a user-defined path style. This script enables users to explicitly specify the desired path style (Unix or Windows), offering flexibility and consistency across different operating systems.
118+
119+
Change the --path_style parameter as needed, '/' for unix like path formatting and '\' for windows like path format.
120+
121+
```bash
122+
python dirtree.py rel /path/to/directory --path_style /
123+
```
124+
125+
**Example Output:**
126+
127+
```bash
128+
directory
129+
directory/file1.txt
130+
directory/file2.txt
131+
directory/subdirectory
132+
directory/subdirectory/file3.txt
133+
```
134+
135+
### Directory Tree Structure:
136+
137+
Display a structured tree view of the directory.
138+
139+
```bash
140+
python dirtree.py tree /path/to/directory
141+
```
142+
143+
**Example Output:**
144+
145+
```bash
146+
+-- directory/
147+
+-- file1.txt
148+
+-- file2.txt
149+
+-- subdirectory/
150+
+-- file3.txt
151+
```
152+
153+
### Beautify Tree Structure:
154+
155+
Display a beautified tree view of the directory.
156+
157+
```bash
158+
python dirtree.py beautify /path/to/directory
159+
```
160+
161+
**Example Output:**
162+
163+
```bash
164+
- directory/
165+
|- file1.txt
166+
|- file2.txt
167+
|- subdirectory/
168+
|- file3.txt
169+
```
170+
171+
### Notes
172+
173+
- Adjust the `/path/to/directory` to the actual path you want to visualize.
174+
- Use the `--path_style` option to specify the path style for formatting (auto, /, \).
175+
- Choose the desired function based on your visualization requirements.

‎Directory Tree Visualizer/dirtree.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import os
2+
import argparse
3+
4+
# Define global constants
5+
# PROJECT_PATH = 'C:/Users/user/Documents/Python/MyProject' # Replace with the actual path to your project
6+
PROJECT_PATH = 'E:/my-nodejs-app'
7+
8+
def simple_tree(dir_path):
9+
print(os.path.basename(dir_path) + '/')
10+
11+
for item in os.listdir(dir_path):
12+
item_path = os.path.join(dir_path, item)
13+
if os.path.isdir(item_path):
14+
simple_tree(item_path)
15+
else:
16+
print(item)
17+
18+
# Platform-Independent Path Handling for Absolute Path Tree
19+
def absolute_path_tree_pi(dir_path):
20+
print(dir_path)
21+
for item in os.listdir(dir_path):
22+
item_path = os.path.join(dir_path, item)
23+
if os.path.isdir(item_path):
24+
absolute_path_tree_pi(item_path)
25+
else:
26+
print(item_path)
27+
28+
# User-Defined Path Formatting for Absolute Path Tree
29+
def absolute_path_tree(dir_path, path_style='auto'):
30+
if path_style == 'auto':
31+
path_style = os.path.sep # Use the default separator based on the platform
32+
33+
print(dir_path.replace(os.path.sep, path_style))
34+
for item in os.listdir(dir_path):
35+
item_path = os.path.normpath(os.path.join(dir_path, item))
36+
if os.path.isdir(item_path):
37+
absolute_path_tree(item_path, path_style)
38+
else:
39+
print(item_path.replace(os.path.sep, path_style))
40+
41+
# Platform-Independent Path Handling for Relative Path Tree
42+
def relative_path_tree_pi(dir_path, relative_path=""):
43+
current_path = os.path.join(relative_path, os.path.basename(dir_path))
44+
print(current_path)
45+
46+
for item in os.listdir(dir_path):
47+
item_path = os.path.join(dir_path, item)
48+
if os.path.isdir(item_path):
49+
relative_path_tree_pi(item_path, current_path)
50+
else:
51+
print(os.path.join(current_path, item))
52+
53+
# User-Defined Path Formatting for Relative Path Tree
54+
def relative_path_tree(dir_path, relative_path="", path_style='auto'):
55+
if path_style == 'auto':
56+
path_style = os.path.sep # Use the default separator based on the platform
57+
58+
current_path = os.path.join(relative_path, os.path.basename(dir_path))
59+
print(current_path.replace(os.path.sep, path_style) + path_style)
60+
61+
for item in os.listdir(dir_path):
62+
item_path = os.path.join(dir_path, item)
63+
if os.path.isdir(item_path):
64+
relative_path_tree(item_path, current_path, path_style) # Use current_path instead of os.path.join(current_path, item)
65+
else:
66+
print(os.path.join(current_path, item).replace(os.path.sep, path_style))
67+
68+
def directory_tree_structure(dir_path, indent=''):
69+
print(indent[:-1] + '+-- ' + os.path.basename(dir_path) + '/')
70+
indent += ' '
71+
72+
for item in os.listdir(dir_path):
73+
item_path = os.path.join(dir_path, item)
74+
if os.path.isdir(item_path):
75+
directory_tree_structure(item_path, indent)
76+
else:
77+
print(indent + '+-- ' + item)
78+
79+
def beautify_tree_structure(dir_path, indent=''):
80+
print(f"{indent}- {os.path.basename(dir_path)}")
81+
82+
for item in os.listdir(dir_path):
83+
item_path = os.path.join(dir_path, item)
84+
if os.path.isdir(item_path):
85+
beautify_tree_structure(item_path, indent + ' |')
86+
else:
87+
print(f"{indent} |- {item}")
88+
89+
def main():
90+
parser = argparse.ArgumentParser(description='Directory Tree Visualizer')
91+
92+
# Specify the function to execute (simple, abs_pi, rel_pi, abs, rel, tree, beautify)
93+
parser.add_argument('function', choices=['simple', 'abs_pi', 'rel_pi', 'abs', 'rel', 'tree', 'beautify'],
94+
help='Function to execute. Choose one of the following: simple, abs_pi, rel_pi, abs, rel, tree, beautify.')
95+
96+
# Specify the path parameter
97+
parser.add_argument('path', help='Path parameter. Enter the path to the target directory.')
98+
99+
# Specify an optional argument for path style formatting
100+
parser.add_argument('--path_style', default='auto',
101+
help='Path style for formatting. Choose one of the following: auto, /, \\ (default: auto).\n'
102+
'Examples:\n'
103+
' - For Unix-like paths: --path_style /\n'
104+
' - For Windows paths: --path_style \\\n'
105+
' - Automatic detection based on the platform: --path_style auto')
106+
107+
args = parser.parse_args()
108+
109+
if not os.path.exists(args.path):
110+
print("Error: The specified path does not exist.")
111+
else:
112+
base_path = os.path.abspath(args.path)
113+
114+
if args.function == 'simple':
115+
# Simple tree
116+
simple_tree(base_path)
117+
elif args.function == 'abs_pi':
118+
# Platform-Independent Path Handling for absolute path tree
119+
absolute_path_tree_pi(base_path)
120+
elif args.function == 'rel_pi':
121+
# Platform-Independent Path Handling for relative path tree
122+
relative_path_tree_pi(base_path)
123+
elif args.function == 'abs':
124+
# User-Defined Path Formatting for absolute path tree
125+
absolute_path_tree(base_path, args.path_style)
126+
elif args.function == 'rel':
127+
# User-Defined Path Formatting for relative path tree
128+
relative_path_tree(base_path, path_style=args.path_style)
129+
elif args.function == 'tree':
130+
# Directory Tree Structure
131+
directory_tree_structure(base_path)
132+
elif args.function == 'beautify':
133+
# Beautify Tree Structure
134+
beautify_tree_structure(base_path)
135+
136+
if __name__ == '__main__':
137+
main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /