Python Forum
exception for EXDEV error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
exception for EXDEV error
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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
Reply
#3
your code makes look clear. thank you.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020