Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file.close() exception handling inside a with statement in Python

I know that in Python the file.close() method doesn't have any return value, but I can't find any information on whether in some cases it throws an exception. If it doesn't do that either, then I guess the second part of this question is superfluous.

If it does, then what would be the "correct" way to handle the file.close() method throwing an exception inside a "with" statement used to open the file?

Is there a situation where file.close() could fail immediately after a file has been open and read from successfully?

like image 413
Bitrex Avatar asked Aug 01 '15 21:08

Bitrex


2 Answers

Yes, file.close() can throw an IOError exception. This can happen when the file system uses quotas, for example. See the C close() function man page:

Not checking the return value of close() is a common but nevertheless serious programming error. It is quite possible that errors on a previous write(2) operation are first reported at the final close(). Not checking the return value when closing the file may lead to silent loss of data. This can especially be observed with NFS and with disk quota.

A non-zero return value of the C close() function leads to Python raising an IOError exception.

If you wanted to handle this exception, put a try...except block around the with statement:

try:
    with open(filename, mode) as fileobj:
        # do something with the open file object
except IOError as exc:
    # handle the exception

Of course, the IOError could also have been thrown during opening.

like image 184
Martijn Pieters Avatar answered Nov 09 '22 00:11

Martijn Pieters


close can throw exceptions, for example, if you run out of disk space while it's trying to flush your last writes, or if you pulled out the USB stick the file was on.

As for the correct way to deal with this, that depends on the details of your application. Maybe you want to show the user an error message. Maybe you want to shut down your program. Maybe you want to retry whatever you were doing, but with a different file. Whatever the response you choose, it'll probably be implemented with a try-except block in whatever layer of your program is best equipped to deal with it.

like image 23
user2357112 supports Monica Avatar answered Nov 09 '22 01:11

user2357112 supports Monica