Yes, file.close() can throw an IOError exception. This can happen if 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 nonetheless serious programming error. It is possible that errors in the previous write(2) operation are first reported in the final close() . Do not check the return value when closing the file can lead to silent data loss. This is especially true with NFS and disk quota.
The non-zero return value of the C close() function causes Python to throw an IOError exception.
If you want to handle this exception, put a try...except block around the with statement:
try: with open(filename, mode) as fileobj:
Of course, an IOError could also be thrown at the time of opening.
source share