By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,259 Members | 1,333 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,259 IT Pros & Developers. It's quick & easy.

Exceptions when closing a file

P: n/a
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/pyt...er/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?
--
Steven.

Mar 20 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Mar 20, 12:25 pm, Steven D'Aprano
<s...@REMOVE.THIS.cybersource.com.auwrote:
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/pyt...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?

--
Steven.
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

Mar 20 '07 #2

P: n/a
Steven D'Aprano <st***@REMOVE.THIS.cybersource.com.auwrote:
>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] rr****@csclub.uwaterloo.ca
-()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
db //
Mar 20 '07 #3

P: n/a
Ross Ridge wrote:
Steven D'Aprano <st***@REMOVE.THIS.cybersource.com.auwrote:
>>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?
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
Mar 20 '07 #4

P: n/a
ky******@gmail.com a écrit :
On Mar 20, 12:25 pm, Steven D'Aprano
<s...@REMOVE.THIS.cybersource.com.auwrote:
>>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/pyt...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?

--
Steven.


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.

Mar 20 '07 #5

P: n/a
On Mar 20, 4:26 pm, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
kyoso...@gmail.com a écrit :
On Mar 20, 12:25 pm, Steven D'Aprano
<s...@REMOVE.THIS.cybersource.com.auwrote:
>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/pyt...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?
>--
Steven.
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.
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

Mar 20 '07 #6

P: n/a
John Nagle <na***@animats.comwrites:
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.

--
\ "Ours is a world where people don't know what they want and are |
`\ willing to go through hell to get it." -- Donald Robert Perry |
_o__) Marquis |
Ben Finney

Mar 20 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.