Python Forum

Full Version: exception for EXDEV error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i am doing os.rename() or os.link() which may sometimes cause (on POSIX) system error EXDEV for which there is no specific exception defined in Python. i don't understand the exception system well enough to understand how this kind of exception is handled. when i try it unhandled i get OSError and a correct error message. i just want to handle this error a specific way (such as copying the file) in my script. i've been read about the exception system in the reference document, but it's not saying enough. searches on 3 searches engines just don't hit a case like this (where no exception is defined).

i am currently making a script that works like the mv or ln commands, but creates a directory at the named target.
An OSError instance has an errno attribute which is the numeric code from the C variable errno. This could perhaps be used here (although there may be portability issues).

You could combine this with the contents of the errno module where there are constants such as errno.EXDEV.
import errno

try:
    do_this()
except OSError as exc:
    if exc.errno == errno.EXDEV:
        do_exdev_action()
    else:
        raise
your code makes look clear. thank you.