-
Notifications
You must be signed in to change notification settings - Fork 15
/
create_index.py
51 lines (41 loc) · 1.38 KB
/
create_index.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
import os
def generate_index_html(directories, output_file):
# Start the HTML content
html_content = """<!DOCTYPE html>
<html>
<head>
<title>Directory Listing</title>
</head>
<body>
<h1>Directory Listing</h1>
<ul>
"""
for directory in directories:
if os.path.isdir(directory):
html_content += f"<li><strong>{directory}</strong></li><ul>"
all_files = []
for root, dirs, files in os.walk(directory):
for filename in files:
file_path = os.path.join(root, filename)
all_files.append(file_path)
# Sort files alphabetically
all_files.sort()
for file_path in all_files:
# Get the relative path to the file from the base directory
relative_path = os.path.relpath(file_path, start=directory)
html_content += f'<li><a href="{directory}/{relative_path}">{relative_path}</a></li>'
html_content += "</ul>"
# Close the HTML content
html_content += """
</ul>
</body>
</html>
"""
# Write the HTML content to the output file
with open(output_file, "w") as f:
f.write(html_content)
# Specify the directories to list
directories_to_list = ["files"]
# Specify the output HTML file
output_html_file = "index.html"
generate_index_html(directories_to_list, output_html_file)