Update (December 21, 2008): I tried running the code in this blog post today, and I was unable to reproduce this bug using Python 2.5.2. Therefore, I don’t know how useful the information in this post is. I have decided to leave this post up in the off chance that it might help somebody anyway.
I spent a couple of hours today trying to figure out this bizarre (and unhelpful) Python error message: IOError: [Errno 0] Error
Here is some simple code which reproduces the error:
fp = open("test.txt", "r+")
while True:
line = fp.readline()
if not line:
break
fp.write("some new text")
It seems that once readline reaches the end of the file, you can no longer write to that file.
Note that you can get the same error if you try to read and write a config file using the ConfigParser module:
import ConfigParser
#read config file
config = ConfigParser.ConfigParser()
configfile = open("temp.config", "r+")
config.readfp(configfile)
#try to write to same file
config.set("Main", "tempDir", "C:\\temp")
config.write(configfile)
configfile.close()
You can get around this error by closing the file after calling the readfp method, and then reopening the file if you want to write to it.
import ConfigParser
#read config file
config = ConfigParser.ConfigParser()
configfile = open("temp.config", "r")
config.readfp(configfile)
configfile.close()
#write config file
config.set("Main", "tempDir", "C:\\temp")
configfile = open("temp.config", "w")
config.write(configfile)
configfile.close()
November 9, 2009 at 4:15 pm
I have experienced the same problem.
Thanks for the workaround.