简体   繁体   English

Python正确的方法来捕获文件关闭的异常

[英]Python proper way to catch exceptions on file close

I'm old at Perl and new to Python. 我在Perl很老,也是Python的新手。 I know in Perl that fd.close() isn't irrelevant. 我在Perl中知道fd.close()并不是无关紧要的。 Writing to a full file system, close() will report the error. 写入完整文件系统时,close()将报告错误。 Also for socket errors, they appear in close(). 同样对于套接字错误,它们出现在close()中。 So how to do with in Python? 那么如何在Python中做什么? Some examples show putting the open() and close() in the same try block which would catch IOError on either. 一些示例显示将open()和close()放在同一个try块中,它会捕获IOError。 But other examples show close() in the finally block to close the file upon exception. 但是其他示例在finally块中显示close()以在异常时关闭文件。 However, what if the exception first occurs in close()? 但是,如果异常首先发生在close()中怎么办?

Does this cover both requirements? 这是否涵盖了这两个要求? (1) Always close the file (2) Catch all IO exceptions? (1)始终关闭文件(2)捕获所有IO异常?

try:
    with open(FILE, 'w') as fd:
        .....
except IOError as err:
    .....

Thanks, Chris 谢谢,克里斯

Your code is correct; 你的代码是正确的; it can't differentiate errors on open from errors on the (implicit) close when the with block exits (nor differentiate errors from any other file operations in the block), but it will catch all such errors. with块退出时(或者不区分块中任何其他文件操作的错误),它无法区分open时的错误和(隐式) close上的错误,但它会捕获所有这些错误。 By the time you reach the except block, you're guaranteed that the file tried to close (the with auto-close will occur before you reach it, whether by fallthrough or an exception being raised), though if the exception occurred during close your options are limited (because recovering meaningfully from close failing is usually not possible). 到时候你到达except块,你保证该文件试图 close (在with你到达之前,自动关闭会发生,无论是通电路或被提出一个例外),但如果期间发生异常close你选项是有限的(因为通常不可能从close失败中有意义地恢复)。

Note that IOError is not exactly correct; 请注意, IOError不完全正确; on Py3 it will work as expected (it's an alias of OSError , which is the actual base exception you want to catch), while on Python 2 it's separate from OSError , which means you won't catch OSError or its subclasses (which are commonly seen on Windows systems in particular). 在Py3上它将按预期工作(它是OSError的别名,这是你想要捕获的实际基本异常),而在Python 2上它与OSError分开,这意味着你不会捕获OSError或其子类(通常是特别是在Windows系统上看到的)。

If you want to definitely catch all such errors portably, you want to catch EnvironmentError , which is a superclass of both IOError and OSError on Python 2, and an alias for OSError on Python 3; 如果你想绝对捕捉所有这些错误可移植的,要赶EnvironmentError ,这是两者的超IOErrorOSError关于Python 2,和一个别名OSError关于Python 3; if portability is not a concern, then OSError is the name that's actually used on Py3, so you may as well use the documented name. 如果可移植性不是问题,那么OSError是Py3上实际使用的名称,因此您也可以使用记录的名称。

Check this answer , one comment says you can open the file separately to find the error on open, then use it as a context manager with try around it again like that: 检查这个答案 ,一条评论说您可以单独打开文件以在打开时找到错误,然后将其用作上下文管理器,再次尝试它:

try:
    f = open( ... )
except IOError:
    ...
try:
    with f:
        ...
except IOError:
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM