Message75766
Here's the version of this that I've been using for almost a decade now:
http://allmydata.org/trac/pyutil/browser/pyutil/pyutil/fileutil.py?rev=127#L241
Actually I used to have a bigger version that could optionally require
certain things of the mode of the directory, but it turned out that I
wasn't going to need it.
def make_dirs(dirname, mode=0777):
"""
An idempotent version of os.makedirs(). If the dir already exists, do
nothing and return without raising an exception. If this call
creates the
dir, return without raising an exception. If there is an error that
prevents creation or if the directory gets deleted after make_dirs()
creates
it and before make_dirs() checks that it exists, raise an exception.
"""
tx = None
try:
os.makedirs(dirname, mode)
except OSError, x:
tx = x
if not os.path.isdir(dirname):
if tx:
raise tx
raise exceptions.IOError, "unknown error prevented creation of
directory, or deleted the directory immediately after creation: %s" %
dirname # careful not to construct an IOError with a 2-tuple, as that
has a special meaning... |
|
Date |
User |
Action |
Args |
2008-11-11 22:59:36 | zooko | set | recipients:
+ zooko, gvanrossum, ggenellina, draghuram, ijmorlan |
2008-11-11 22:59:36 | zooko | set | messageid: <1226444376.44.0.64147057752.issue1675@psf.upfronthosting.co.za> |
2008-11-11 22:59:35 | zooko | link | issue1675 messages |
2008-11-11 22:59:34 | zooko | create | |
|