[Python-Dev] mkdir -p in python

Steven D'Aprano steve at pearwood.info
Tue Jul 20 15:09:59 CEST 2010


On Tue, 20 Jul 2010 06:49:44 pm 岳帅杰 wrote:
> Sorry, I don't know what is the "no constant arguments" guideline
> refers to. Could you give me some more explanation?

It refers to the guideline that you shouldn't have a single function 
with two (or more) different behaviour and an argument that does 
nothing but select between them. Here is a toy example:

def change_case(s, make_upper=True):
    if make_upper:
        return s.upper()
    else:
        return s.lower()


> By the way, I feel adding separate functions is not quiet worthy for
> such a function.

I tend to agree. Perhaps all we need is a recipe in the docs:

try:
    os.makedirs(path)
except OSError, e:
    if e.errno != 17:
        raise

I think that's all it takes to get the behaviour wanted without any 
false negatives, and four lines is short enough to use in-place, or if 
you prefer it's easy enough to wrap it in a function in your own 
module. Not everything needs to be a built-in.

I'm -0 on adding an argument to os.makedirs, +0 on adding a variant 
function to os, and +0.5 on adding the variant to the shutil module.


-- 
Steven D'Aprano


More information about the Python-Dev mailing list