Exceptions when closing a file

S

Steven D'Aprano

Closing a file can (I believe) raise an exception. Is that documented
anywhere? I've spent a lot of frustrating time trying to track this down,
with no luck, which suggests that either my google-foo is weak or that it
isn't documented. Is IOError the only exception it can raise?

The only thing I have found is this:

http://mail.python.org/pipermail/python-bugs-list/2004-November/026031.html

Out of curiosity, is there a simple way to demonstrate close() raising an
exception that doesn't involve messing about with disk quotas?
 
R

Ross Ridge

Steven D'Aprano said:
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
 
K

kyosohma

Closing a file can (I believe) raise an exception. Is that documented
anywhere? I've spent a lot of frustrating time trying to track this down,
with no luck, which suggests that either my google-foo is weak or that it
isn't documented. Is IOError the only exception it can raise?

The only thing I have found is this:

http://mail.python.org/pipermail/python-bugs-list/2004-November/02603...

Out of curiosity, is there a simple way to demonstrate close() raising an
exception that doesn't involve messing about with disk quotas?

I've never had any problems closing a file. Maybe you need to flush it
before you close it? Are you running threads that access the file in
ad hoc fashion or something else out of the ordinary? Is this some
sort of long running process writing a large file?

Mike
 
J

John Nagle

Ross said:
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.

Closing a file that's being written can, of course, fail.
An I/O error is possible as the file is flushed to disk.

This is useful; after the close has returned, you have
some confidence that the file has been fully written.

If you want to force this error, write to a drive
reached over a network, or a removable medium like a floppy
or flash card. Open a file for writing and disconnect the
network or remove the removable medium.

John Nagle
 
K

kyosohma

(e-mail address removed) a écrit :





Usually, closing the file flushes the buffer. At least with buffered
files, but that's the most common situation AFAICT.


Might just be that there's no more available storage space.

Very true...I was fishing for more information to be able to give a
more specific answer. And I was tired, so I didn't think of
everything. Good call, you guys!

Mike
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
I've never had any problems closing a file. Maybe you need to flush it
before you close it?

Usually, closing the file flushes the buffer. At least with buffered
files, but that's the most common situation AFAICT.
Are you running threads that access the file in
ad hoc fashion or something else out of the ordinary? Is this some
sort of long running process writing a large file?

Might just be that there's no more available storage space.
 
B

Ben Finney

John Nagle said:
If you want to force [an error while closing a file], write to
a drive reached over a network, or a removable medium like a floppy
or flash card. Open a file for writing and disconnect the network
or remove the removable medium.

Or simply use a mock library, create a mock file object, and guarantee
(for the purpose of your unit test) that it *will* raise the
appropriate error without interacting with the rest of the system.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,172
Messages
2,570,937
Members
47,473
Latest member
pioneertraining

Latest Threads

Top