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.
April 19, 2010 at 12:31 am
Just ran into this myself. It seems to be a general problem with Python 2.x on Windows when doing interleaved reads and writes on a file which has been opened in a “+” mode. It’s caused by the behavior of the underlying C library. The general workaround is to insert a flush() or seek() operation when switching between reading and writing.
Python 3.x doesn’t have this problem, because the underlying I/O implementation was changed.
See the following bug reports:
http://bugs.python.org/issue3207
http://bugs.python.org/issue1636874
July 2, 2010 at 7:48 pm
I’ve experienced the same bug today on python 2.6.5.12 32-bit.
I think there is something underneath causes this problem. Error message is not descriptive. Maybe the problem is I’ve created file at just under c:\ folder. there may be UAC restrictions… I’ll post here again when I find a soln. Please contact me if you find any, too.
July 2, 2010 at 7:52 pm
Most probably the problem causes from r+ mode. If you separate read stream and write stream, problem is solved.
November 1, 2010 at 11:09 am
You need to move the file pointer to the insertion point of the write. Just do f.seek(0) to move the file pointer back to the beginning…
November 24, 2010 at 8:20 pm
Thanks for writing about this bug! You saved me a lot of time.
many thanks.