Semantic of operations on a closed file object
Baptiste Lepilleur
baptiste.lepilleur at gmail.com
Sat Dec 26 09:44:19 EST 2009
Reading python io.IOBase class documentation, I'm kind of confused at the
expected behavior of operation on a closed file object.
The io.IOBase class doc says:
"""Note that calling any method (even inquiries) on a closed stream is
undefined. Implementations may raise
IOError<exceptions.html#exceptions.IOError>in this case."""
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?
>>> with open( 'dummy', 'wb') as f:
... pass
...
>>> f.write( b'' )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: write to closed file
>>> f.writable()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file
>>> import io
>>> s = io.StringIO()
>>> s.close()
>>> s.read()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091226/4c2e9274/attachment-0001.html>
More information about the Python-list
mailing list