Skip to content Skip to sidebar Skip to footer

Are Python Error Numbers Associated With Ioerror Stable?

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: tr

Solution 1:

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

This makes your code less likely to break in case the integer error value changes (although that is unlikely to occur).

Post a Comment for "Are Python Error Numbers Associated With Ioerror Stable?"