Using Python to write HTML
# Makes a home page in a file you name.
# Careful! if the file exists already, it will be overwritten!!!
# To test this safely, use a text editor to create an empty text file
# Then call it with makeHomePage(pickAFile(), "Chipper Jones", "baseball") for example
# making sure that you pick the empty file that you just created.
def makeHomePage(fname, name, interest):
# Generate the home page from boilerplate HTML and spliced-in name and interest.
html="""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transition//EN" "http://wwww.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>""" + name + """'s Home Page</title>
</head>
<body>
<h1>Welcome to """ + name + """'s Home Page</h1>
<p>Hi! I am """ + name + """. This is my home page!
I am interested in """ + interest + """</p>
</body>
</html>"""
# Save the home page to disk
# writeTextTo is analogous to writePictureTo and writeSoundTo, but unlike them, it isn't part of JES
# It is defined below.
writeTextTo(html, fname)
# writeTextTo saves a text to a file.
# text is the text you are saving to a file
# fileName is the complete path to the file
def writeTextTo(text, fileName):
# Open a text file for writing.
file = open(fileName, "wt")
# Write the text into the file.
file.write(text)
# Tidy up after yourself
file.close()
Link to this Page