[Tutor] how to catch particular Errno for Exceptions

Brian van den Broek bvande at po-box.mcgill.ca
Mon Apr 5 19:01:47 EDT 2004


Hi all,

how do I catch a specific Errno for an exception?

I have no G: drive and have tried the following code:

import os

try:
     os.mkdir('g:\\silly')
except OSError:
     print 'OSError caught!'

try:
     os.mkdir('gGg:\\sillier')
except OSError, (2,):  #what to write here?
     print 'OSError Errno 2 caught!'

The first works try clause just fine, the second gives me a "can't 
assign to a literal" error when I try to run it out of IDLE. I've 
tried "except OSError, 2", "except OSError, (2)" and various other
combinations, but no luck. Consulting the Tutorial, the Language
Reference, and Learning Python has not cleared it up for me.

Also, I've just posted three questions all on related, but
distinct topics. Most list I am on prefer 3 msgs in such
situations. Would it be preferred that I had posted all in one?
(The last thing I want to do is annoy all the generous and patient
Python tutors!)

Thanks and best,

Brian vdB








More information about the Tutor mailing list