与IO错误稳定相关的Python错误号码? [英] Are Python error numbers associated with IOError stable?
问题描述
我要移动的文件,但在情况下,它没有找到我应该忽略它。在其他情况下的异常应被传播。我有下面这段Python的code的:
I want to move a file, but in the case it is not found I should just ignore it. In all other cases the exception should be propagated. I have the following piece of Python code:
try:
shutil.move(old_path, new_path)
except IOError as e:
if e.errno != 2: raise e
错误号== 2是一个,有没有这样的文件或目录的说明。我不知道这是不同的Python版本和平台,等。
errno == 2 is the one, that has 'No such file or directory' description. I wonder if this is stable across Python versions and platforms, and etc.
推荐答案
这是更好地从的错误号模块,而不是硬编码值 2
:
It is better to use values from the errno module instead of hardcoding the value 2
:
try:
shutil.move(old_path, new_path)
except IOError as e:
if e.errno != errno.ENOENT: raise e
这使你的code不太可能的情况下,打破了整误差值的变化(尽管这不太可能发生)。
This makes your code less likely to break in case the integer error value changes (although that is unlikely to occur).
这篇关于与IO错误稳定相关的Python错误号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!