Semantic of operations on a closed file object

Antoine Pitrou solipsis at pitrou.net
Sat Dec 26 12:37:09 EST 2009


Hello,

> But the io.IOBase.close() method document says: """Once the file is
> closed, any operation on the file (e.g. reading or writing) will raise
> an IOError <exceptions.html#exceptions.IOError>.""" which unlike the
> class doc is not conditional about the behavior...
> 
> Experimentation (see below) show that I get a ValueError in practice
> (python 3.1) with io.BufferedWriter and io.StringIO objects.
> 
> So which one is right? Am I reading the wrong documentation?

The documentation is wrong in this case. ValueError is indeed returned, 
as is already the case in Python 2.x:

Python 2.6.4 (r264:75706, Oct 27 2009, 15:50:27) 
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f =open('foo', 'w')
>>> f.close()
>>> f.write('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file


You could/should open a bug about this on http://bugs.python.org so that 
it isn't forgotten.

Thank you

Antoine.




More information about the Python-list mailing list