-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
62 lines (54 loc) · 2.75 KB
/
main.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
import os
def list_files(startpath):
markdown = "# Directory Structure of `{}`\n\n".format(startpath)
startdir_name = os.path.basename(startpath)
for root, dirs, files in os.walk(startpath):
level = root.replace(startpath, '').count(os.sep)
indent = ' ' * 4 * (level)
folder_name = os.path.basename(root)
folder_path = os.path.join(startdir_name, os.path.relpath(root, startpath))
markdown += '{}- [{}](./{})\n'.format(indent, folder_name, folder_path.replace('\\', '/'))
subindent = ' ' * 4 * (level + 1)
for f in files:
file_path = os.path.join(folder_path, f)
markdown += '{}- [{}](./{})\n'.format(subindent, f, file_path.replace('\\', '/'))
return markdown
# ASCII Art
print("""
____ _ _ _ _____ _ _
| \|_|___ ___ ___| |_ ___ ___ _ _ | |_ ___ | |___ ___| |_ _| |___ _ _ _ ___
| | | | _| -_| _| _| . | _| | | | _| . | | | | | .'| _| '_| . | . | | | | |
|____/|_|_| |___|___|_| |___|_| |_ | |_| |___| |_|_|_|__,|_| |_,_|___|___|_____|_|_|
|___|
""")
while True:
# ask for directory
directory = input("\nEnter the directory (or 'quit' to stop): ")
if directory.lower() == 'quit':
break
if not os.path.exists(directory):
print("The directory does not exist. Please try again.")
continue
# ask for output file name
filename = input("Enter the output filename (without .md extension): ")
filename = filename.strip() + ".md"
# ask for saving directory
save_directory = input("Press Enter to save in the current directory or provide a directory to save to: ").strip()
if save_directory and not os.path.exists(save_directory):
print("The provided save directory does not exist. Please try again.")
continue
markdown = list_files(directory)
save_path = filename if not save_directory else os.path.join(save_directory, filename)
with open(save_path, 'w') as f:
f.write(markdown)
print("""
_______ __ __
| _ .-----.-----.-----.----.---.-| |_.-----.--| |
|. |___| -__| | -__| _| _ | _| -__| _ |
|. | |_____|__|__|_____|__| |___._|____|_____|_____|
|: 1 |
|::.. . |
`-------'
""")
print("\nThe directory structure has been saved as '{}'.".format(filename))
print("\n Bye!")