Make a Local Link List with Python
A site with several hand-written pages can use a little help keeping its index up to date. This Python exercise takes the HTML filenames in a folder and prints a basic list. It’s written for modern Python so you can run it today while recreating the kind of housekeeping script that was useful in 2005.
from html import escape
from pathlib import Path
from urllib.parse import quote
print("<ul>")
for page in sorted(Path(".").glob("*.html")):
if page.is_file():
print(f'<li><a href="{quote(page.name)}">{escape(page.stem)}</a></li>')
print("</ul>")
Save the script as links.py in a folder with two sample pages. Run python3 links.py and look at the printed markup. Add a page whose filename contains a space. The URL should encode the space, while the displayed text stays readable. Copy the output into a test page in the same directory and click the links.