[Python-Dev] UserString

Barry Warsaw barry at python.org
Tue Feb 22 03:50:01 CET 2005


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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 307 bytes
Desc: This is a digitally signed message part
Url : http://mail.python.org/pipermail/python-dev/attachments/20050221/ae2d9387/attachment.pgp


More information about the Python-Dev mailing list