detecting and creating directories

Steve Holden sholden at holdenweb.com
Mon Oct 29 14:12:45 EST 2001


"Angel Asencio" <asencio at mitre.org> wrote in ...
> Greetings:
>
>   I found the module that has the command to make directories, but I
> have not been lucky finding a way  to check for the existance of a
> directory (so it is not there, I can create it).
>
> Any pointers?
>
You should also check that it really is a directory and not just a plain
file! The following code is untested...

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)

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list