Skip to content

Instantly share code, notes, and snippets.

@gh640
Created March 6, 2025 10:26
Show Gist options
  • Save gh640/6f5d9652745574167156bba375f77f08 to your computer and use it in GitHub Desktop.
Save gh640/6f5d9652745574167156bba375f77f08 to your computer and use it in GitHub Desktop.
Python: カレントディレクトリの HTML ファイル一覧を表示する HTML ファイルを生成するスクリプト
"""カレントディレクトリの HTML ファイル一覧を表示する HTML ファイルを生成するスクリプト"""
import html
from pathlib import Path
def main():
html_files = sorted(p.name for p in Path(".").glob("*.html"))
html_files = (html.escape(x) for x in html_files)
file_list = [f'<li><a href="{x}">{x}</a></li>' for x in html_files]
html_content = "\n".join(
[
"<!DOCTYPE html>",
"<html lang='ja'>",
"<head>",
"<meta charset='UTF-8'>",
"<title>HTML ファイル一覧</title>",
"</head>",
"<body>",
"<h1>HTML ファイル一覧</h1>",
"<ul>",
"{}",
"</ul>",
"</body>",
"</html>",
]
)
print(html_content.format("\n".join(file_list)))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment