4

I'd like to know how to handle different exceptions that have the same "type". I'm trying to use some code to make a directory:

os.mkdir(target_dir_name)

And I know this can fail for a variety of reasons, for example if the directory already exists:

OSError: [Errno 17] File exists: 

or if there's no permissions to create a new dir:

OSError: [Errno 13] Permission denied:

I'd like to tailor my error messages to the specific failure cause, so I came up with the following code:

try:
  os.mkdir(target_dir_name)
except OSError as e:
  if e.errno == 17:
    print "Warning: Directory %s already exists, executing a rebuild" % (target_dir_name)
  elif e.errno == 13:
    sys.exit("Error: Directory "+target_dir_name+" cannot be created incorrect permissions")

but I'd like to do something a little less hardcoded. Is there a Pythonic way I could update my sub-failure checks?

4
  • In Python 3.3, there errors got split into distinct types: docs.python.org/3/whatsnew/… Commented Jul 10, 2013 at 18:10
  • I haven't found an elegant solution yet since a lot of those exception and error types are so specific with their attributes. So generally in this case I usually do what stackoverflow.com/a/16268642/158111 suggests - wrap specific error handling into a function and avoid repetitive hardcoding. Commented Jul 10, 2013 at 18:12
  • @delnan - Thanks for the input. This will be a distributed script when I'm done with it and I'm trying not to tie myself down to one version of Python if I can help it. I can't guarantee what everyone will be running, but I figured 2.7.3 would be pretty safe (as that comes standard on the latest stable Ubuntu release) Commented Jul 10, 2013 at 18:12
  • 2.7.3 is almost as version-locked as you can get. The 2.7 line is the final version of Python 2. Python 2.7.4 and 2.7.5 are bugfix releases, and Python 3 breaks backwards compatibility. Commented Jul 10, 2013 at 18:30

1 Answer 1

1

The only information I've found... which I guess is my best answer:

With the errno module I can at least rewrite my current code as:

import errno
#...
except OSError as e:
  if e.errno == errno.EEXIST:
    print "Warning: Directory %s already exists, e..."
  elif e.errno == errno.EACCESS:
    sys.exit("Error: Directory "+target_dir_name+"..."

If I upgrade to python 3.2 there is the much nicer:

except FileExistsError:
  print ("Warning: Directory %s already exists, e...")
except PermissionError:
  sys.exit("Error: Directory "+target_dir_name+"...")

Seems I could do something fancy with using sys.version_info.major and minor to make sure it's at least 3.2 as well.

Hopefully something better will come up...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.