import re filename = "index.html" newContent = "Hello Python World" filehandle = open(filename, "r") data = filehandle.read() filehandle.close() matching = re.subn("test", newContent, data) if matching[1] == 0: raise "Error while parsing HTML template" print "Content-Type: text/html\n\n" print matching[0]