Tichigan Tichigan - 3 months ago 7
Python Question

Trouble writing to html file with python

I'm having trouble writing a very simple html file in python.

The code is:

filehtml = 'C:\pathhere\helloworld.html'

message = '''<html>
<head></head>
<body><p><Hello World!</p></body>
</html>'''

f = open(filehtml,'w')
f.write(message)
f.close()


The file appears, is about 65 bytes, but when I open it in chrome or firefox it's blank. There's a little lock on the file icon, but when I right click on options, the permissions include me. So...I don't know if this is a code problem or something else. Also, all the other files on my desktop have locks and I open them just fine.
I looked here for help (How to write and save html file in python?)

I'm using Windows 7, python 2.7 Anaconda
Thanks.

Answer

You have an extra < which is messing everything up.

filehtml = 'C:\pathhere\helloworld.html'

message = '''<html>
<head></head>
<body><p>Hello World!</p></body>
</html>'''

f = open(filehtml,'w')
f.write(message)
f.close()