[Python-Dev] mkdir -p in python

Guido van Rossum guido at python.org
Sun Jul 18 11:53:42 CEST 2010

On Sun, Jul 18, 2010 at 11:30 AM, Tim Golden <mail at timgolden.me.uk> wrote:
> On 17/07/2010 11:03 PM, Peng Yu wrote:
>> I don't see that there is a function in the library that mimic the
>> behavior of 'mkdir -p'. If 'makedirs' is used, it will generate an
>> error if the file already exists. There are some functions available
>> on the website to close the gap. But I'd prefer this is in the
>> library. Is there any plan to add a function that mimic the behavior
>> of  'mkdir -p'?
> If I were you I'd ping Tarek who's taken ownership of the shutil
> module. It would go there if it went anywhere.
> That said, it's not clear just how far the stdlib should go to
> mimic every switch and option of shell commands...

Well, it really should have behaved like mkdir -p in the first place.
Eric Raymond submitted the code ages ago, and at the time I wasn't
familiar with mkdir -p, otherwise, I would have made him change it. I
believe every single use of os.mkdirs() I've made goes something like

if not os.path.exists(x):

or perhaps like this:

except os.error:

(Though the latter masks a bunch of other rare but possible failure
modes -- often this is in throw-away code though where it doesn't
matter or the next line will raise an exception that's good enough to
diagnose the situation anyway.)

--Guido van Rossum (python.org/~guido)

More information about the Python-Dev mailing list