0

So I'm trying to generate html files for each day of the year while changing day, month in my template. I've tried the follwing but I get the following error:

Traceback (most recent call last):
  File "c:\Users\Galax\Dokumente\Programming\Python\GenerateBirthdayCounters\main.py", line 23, in <module>
    make_file(str(month),str(day))
  File "c:\Users\Galax\Dokumente\Programming\Python\GenerateBirthdayCounters\main.py", line 9, in make_file
    f.write(html.format(day=day.rjust(2, "0"),month=month.rjust(2, "0"),bg="#660066"))
KeyError: '\n  --smaller'

This is my main code:

import os
from html import html

os.mkdir("pages")
os.chdir("pages")

def make_file(month, day):
    with open(f"{day}.html", "w") as f:
        f.write(html.format(day=day.rjust(2, "0"),month=month.rjust(2, "0"),bg="#660066"))

for month in range(1,13):
    os.mkdir(str(month))
    os.chdir(str(month))
    
    if month == 2:
        for day in range(1,29):
            make_file(str(month),str(day))
    elif month % 2 == 0:
        for day in range(1,32):
            make_file(str(month),str(day))
    else:
        for day in range(1,31):
            make_file(str(month),str(day))
    
    os.chdir("..")

And this is html.py simplified:

html = """
some text
othertext {month} jsd
text {day} anytest
blabla {bg}
"""

complete file see here (if necessary): https://pastebin.com/pX089Cwc

  • https://stackoverflow.com/questions/2755201/str-format-raises-keyerror take a look at this. your problem is caused by the other {} brackets in the html string which aren't supposed to be placeholders. you will have to use double curly brackets where there are no placeholders – Sandil Ranasinghe Feb 19 '22 at 09:11
  • Can't reproduce your problem, neither on Windows, Linux nor macOS? – Timus Feb 19 '22 at 13:42
  • It was because of other curly brakets in my multiline string, had to replace all with double – GalaxyLinus Feb 20 '22 at 18:06

0 Answers0