exception handling in complex Python programs

eliben eliben at gmail.com
Wed Aug 20 07:24:45 CEST 2008

""" between file()
and open() in Python 2 and 3, a NameError is thrown with open() in
Python 3 and an IOError is thrown in the other three cases <bashes
head against keyboard>.

This is *exactly* my concern with Python exceptions. You just never
know what can be thrown at you.

> You want to look up Easier to Ask Forgivness than Permission (EAFP)
> which is touted as the "canonical" error-handling paradigm for Python.

Any (semi)complete guides on this canonical paradigm online ? I've
only found some references in maillist discussions.

>   def do_something(filename):
>     if not os.access(filename,os.R_OK):
>       return err(...)
>     f = open(filename)
>     ...

But does os.access cover absolutely all the errors that can happen
during open() ? What guarantees it, and how can I know without you
teaching me, just from the docs ?

More information about the Python-list mailing list