Created
May 9, 2025 01:58
-
-
Save giswqs/ac2c109d5a879f49191228d602ac1637 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "3a6b1480", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import os\n", | |
"import shutil\n", | |
"\n", | |
"\n", | |
"def generate_index_html(directories, output_file):\n", | |
" # Start the HTML content\n", | |
" html_content = \"\"\"<!DOCTYPE html>\n", | |
"<html>\n", | |
"<head>\n", | |
" <title>Directory Listing</title>\n", | |
"</head>\n", | |
"<body>\n", | |
" <h1>Directory Listing</h1>\n", | |
" <ul>\n", | |
"\"\"\"\n", | |
"\n", | |
" for directory in directories:\n", | |
" if os.path.isdir(directory):\n", | |
" html_content += f\"<li><strong>{directory}</strong></li><ul>\"\n", | |
" all_files = []\n", | |
" for root, dirs, files in os.walk(directory):\n", | |
" for filename in files:\n", | |
" file_path = os.path.join(root, filename)\n", | |
" all_files.append(file_path)\n", | |
"\n", | |
" # Sort files alphabetically\n", | |
" all_files.sort()\n", | |
"\n", | |
" for file_path in all_files:\n", | |
" # Get the relative path to the file from the base directory\n", | |
" if file_path.endswith(\"generate_index.py\"):\n", | |
" # Skip the script itself\n", | |
" continue\n", | |
" relative_path = os.path.relpath(file_path, start=directory)\n", | |
" html_content += f'<li><a href=\"{directory}/{relative_path}\">{relative_path}</a></li>'\n", | |
" html_content += \"</ul>\"\n", | |
"\n", | |
" # Close the HTML content\n", | |
" html_content += \"\"\"\n", | |
" </ul>\n", | |
"</body>\n", | |
"</html>\n", | |
"\"\"\"\n", | |
"\n", | |
" # Write the HTML content to the output file\n", | |
" with open(output_file, \"w\") as f:\n", | |
" f.write(html_content)\n", | |
"\n", | |
"\n", | |
"# Specify the directories to list\n", | |
"directories_to_list = [\".\"]\n", | |
"# Specify the output HTML file\n", | |
"output_html_file = \"index.html\"\n", | |
"\n", | |
"generate_index_html(directories_to_list, output_html_file)\n" | |
] | |
} | |
], | |
"metadata": { | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment