Bizarre Python File Error

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()

Advertisement

6 Responses to “Bizarre Python File Error”

  1. uszy wieloryba Says:

    I have experienced the same problem.

    Thanks for the workaround.

  2. Colin Howell Says:

    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

  3. ahmet alp balkan Says:

    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.

  4. ahmet alp balkan Says:

    Most probably the problem causes from r+ mode. If you separate read stream and write stream, problem is solved.

  5. Ron McLaren Says:

    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…

  6. John Says:

    Thanks for writing about this bug! You saved me a lot of time. :)

    many thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.