On Mon, 2005-02-21 at 11:15, Guido van Rossum wrote:

> Right. There are plenty of examples where LBYL is better, e.g. because
> there are too many different exceptions to catch, or they occur in too
> many places. One of my favorites is creating a directory if it doesn't
> already exist; I always use this LBYL-ish pattern:
> 
>  if not os.path.exists(dn):
>     try:
>        os.makedirs(dn)
>     except os.error, err:
>        ...log the error...
> 
> because the specific exception for "it already exists" is quite subtle
> to pull out of the os.error structure.

Really?  I do this kind of thing all the time:

import os
import errno
try:
    os.makedirs(dn)
except OSError, e:
    if e.errno <> errno.EEXIST:
        raise

-Barry

Attachment: signature.asc
Description: This is a digitally signed message part

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to