比系统自带的更加友好的makedir函数,自带makedir函数,[Python]代码Py


[Python]代码

Python语言: Python Cookbook: 比系统自带的更加友好的makedir函数#from: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/82465def _mkdir(newdir):    """works the way a good mkdir should :)        - already exists, silently complete        - regular file in the way, raise an exception        - parent directory(ies) does not exist, make them as well    """    if os.path.isdir(newdir):        pass    elif os.path.isfile(newdir):        raise OSError("a file with the same name as the desired " \                      "dir, '%s', already exists." % newdir)    else:        head, tail = os.path.split(newdir)        if head and not os.path.isdir(head):            _mkdir(head)        #print "_mkdir %s" % repr(newdir)        if tail:            os.mkdir(newdir)

评论关闭