Home:ALL Converter>Type error writing to file in Python

Type error writing to file in Python

Ask Time:2013-04-01T02:51:59         Author:user118990

Json Formatter

I am writing a Python script to notify me when changes are made to a webpage and store the current state of the page to a file in order to resume seamlessly after rebooting. The code is as follows:

import urllib
url="http://example.com"
filepath="/path/to/file.txt"
try:
    html=open(filepath,"r").read() # Restores imported code from previous session
except:
    html="" # Blanks variable on first run of the script
while True:
    imported=urllib.urlopen(url)
    if imported!=html:
    # Alert me
    html=imported
    open(filepath,"w").write(html)
# Time delay before next iteration

Running the script returns:

Traceback (most recent call last):
  File "April_Fools.py", line 20, in <module>
    open(filepath,"w").write(html)
TypeError: expected a character buffer object

------------------
(program exited with code: 1)
Press return to continue

I've no idea what this means. I'm relatively new to Python. Any help would be much appreciated.

Author:user118990,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/15733178/type-error-writing-to-file-in-python
Martijn Pieters :

urllib.urlopen does not return a string, it returns a response as a file-like object. You need to read that response:\n\nhtml = imported.read()\n\n\nOnly then is html a string you can write to a file.",
2013-03-31T18:53:25
yy