Exceptions when closing a file
Ross Ridge
rridge at caffeine.csclub.uwaterloo.ca
Tue Mar 20 14:02:33 EDT 2007
Steven D'Aprano <steve at REMOVE.THIS.cybersource.com.au> wrote:
>Closing a file can (I believe) raise an exception. Is that documented
>anywhere?
In a catch-all statement for file objects: "When a file operation fails
for an I/O-related reason, the exception IOError is raised." The fact
that close() is a file operation that might fail is revealed by "file
objects are implemented using C's stdio package" and the fact the C's
fclose() function can fail.
> Is IOError the only exception it can raise?
I assume it can raise the exceptions MemoryError and KeyboardInterupt,
which just about any Python operation can raise.
>Out of curiosity, is there a simple way to demonstrate close() raising an
>exception that doesn't involve messing about with disk quotas?
Something like the following should work:
f = file("/dev/null", "r")
os.close(f.fileno)
f.close()
Normally however, you can expect file method close() to fail for all
the same reasons you would expect write() to fail.
Ross Ridge
--
l/ // Ross Ridge -- The Great HTMU
[oo][oo] rridge at csclub.uwaterloo.ca
-()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
db //
More information about the Python-list
mailing list