473,508 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exceptions when closing a file

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
6 1595
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1618
by: Vikram Paranjape | last post by:
Hi, I wrote a small test script to utilize the ScopeGuard class from the Dec 2000 CUJ article. I noted that my cleanup function was being called on normal block exit, but not on calling a...
13
4817
by: Siemel Naran | last post by:
Hi. I posted this question to comp.lang.c++, but am rephrasing it a bit from what I learned and posting to comp.lang.c++.moderated for more insight. So how do I solve my problem? I want it so...
9
2321
by: Gianni Mariani | last post by:
I'm involved in a new project and a new member on the team has voiced a strong opinion that we should utilize exceptions. The other members on the team indicate that they have either been burned...
4
1731
by: Aaron W. LaFramboise | last post by:
I'm seeking a solution to my C++-life long dilemma of how to deal with errors when exceptions aren't appropriate. Below I highlight two error cases, which mainly occur when trying to handle...
6
2817
by: RepStat | last post by:
I've read that it is best not to use exceptions willy-nilly for stupid purposes as they can be a major performance hit if they are thrown. But is it a performance hit to use a try..catch..finally...
17
1474
by: vgrebinski | last post by:
Hi everyone, Suppose we have a diamond shaped diagram: A / \ B1 B2 \ / C and assume that C' constructor invokes
2
2173
by: Bob | last post by:
I MUST be able to trap unhandled exceptions, bring the thread to a routine that then closes the thread on which the execption occurred without closing or affecting the other threads. Think of an...
1
2369
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I...
0
6487
RedSon
by: RedSon | last post by:
Chapter 3: What are the most common Exceptions and what do they mean? As we saw in the last chapter, there isn't only the standard Exception, but you also get special exceptions like...
0
7227
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7127
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7331
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7391
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5633
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3204
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
768
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.