On 25/03/2012 08:22, Russel Winder wrote:
Michael,
On Sat, 2012-03-24 at 15:20 -0700, Michael Lewis wrote:
[...]
It is perhaps worth noting that in Python 3, the syntax has changed:
import os, errno
try:
os.makedirs('a/b/c')
except OSError, e:
except OSError as e :
if e.errno != errno.EEXIST:
raise
This "as" syntax works in 2.6 and 2.7 so is probably the syntax to use
unless you have to use very old versions of Python. I think the "as"
syntax makes it clearer that e is a variable referring to the instance
of OSError that causes the except clause to execute if it does.
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
It's worth noting that PEP 3151 has been implemented in Python 3.3 see
http://docs.python.org/dev/whatsnew/3.3.html#pep-3151-reworking-the-os-and-io-exception-hierarchy
Quoting from the above link.
"Thanks to the new exceptions, common usages of the errno can now be
avoided. For example, the following code written for Python 3.2:
from errno import ENOENT, EACCES, EPERM
try:
with open("document.txt") as f:
content = f.read()
except IOError as err:
if err.errno == ENOENT:
print("document.txt file is missing")
elif err.errno in (EACCES, EPERM):
print("You are not allowed to read document.txt")
else:
raise
can now be written without the errno import and without manual
inspection of exception attributes:
try:
with open("document.txt") as f:
content = f.read()
except FileNotFoundError:
print("document.txt file is missing")
except PermissionError:
print("You are not allowed to read document.txt")
"
--
Cheers.
Mark Lawrence.
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor