detecting and creating directories

Chris Tavares christophertavares at earthlink.net
Mon Oct 29 20:28:21 EST 2001


"Steve Holden" <sholden at holdenweb.com> wrote in message
news:jckD7.1013$0l4.51795 at atlpnn01.usenetserver.com...
> "Fredrik Lundh" <fredrik at pythonware.com> wrote ...
> > Steve wrote:
> >
> > > import os
> > > path = "/usr/tmp/newdir"
> > > if os.path.exists(path):
> > >     if not os.path.isdir(path):
> > >         raise ValueError, "%s exists but is not a directory" % path
> > > else:
> > >     os.mkdir(path)
> >
> > for ten extra points, explain why this is a lousy way to
> > do this.
>
> Because the effbot is inherently clueful, and has suggested this is not
the
> best way?
>
> It would be nice, of course, to ensure that the mkdir() had generated a
> writable directory. Or that an existent directory was writable. Those
cases
> would be handled by the open-for-writing code that created the file,
though.
> I'm sure you have much more to say than just that...
>
> asking-for-it-ly y'rs  - steve
> --

Another problem is, in a multitasking OS, that the directory in question
could have been created between the os.path.exists call and the os.mkdir
call. It's probably best to just call os.mkdir and catch the exception.

-Chris






More information about the Python-list mailing list