-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_directory_structure.py
36 lines (29 loc) · 1.4 KB
/
generate_directory_structure.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
import os
def generate_directory_structure(root_dir, indent=''):
structure = []
items = os.listdir(root_dir)
items.sort() # Sort items to maintain consistent order
for index, item in enumerate(items):
full_path = os.path.join(root_dir, item)
is_last = index == len(items) - 1
marker = '└── ' if is_last else '├── '
structure.append(f"{indent}{marker}{item}")
if os.path.isdir(full_path):
if is_last:
structure.extend(generate_directory_structure(full_path, indent + ' '))
else:
structure.extend(generate_directory_structure(full_path, indent + '│ '))
return structure
def write_directory_structure_to_file(root_dir, output_file):
structure = generate_directory_structure(root_dir)
with open(output_file, 'w', encoding='utf-8') as file:
try:
file.write("\n".join(structure))
except UnicodeEncodeError:
print("Error: Unable to write some characters to the file. Check your encoding settings.")
# Example usage
if __name__ == "__main__":
root_directory = os.path.dirname(os.path.abspath(__file__)) # The directory where this script is located
output_file = os.path.join(root_directory, 'directory_structure.txt')
write_directory_structure_to_file(root_directory, output_file)
print(f"Directory structure written to {output_file}")