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?
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 previouswrite(2)
operation are first reported at the finalclose()
. 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With