def mkdir_p(newdir, mode=0o777):
    """Recursively creates the target directory and all of its parents if they do not
    already exist.  Fails silently if they do.

    :param str newdir: The directory path to ensure
    :raises: OSError if a file is encountered along the way
    """
    # http://code.activestate.com/recipes/82465-a-friendly-mkdir/

    newdir = fs_encode(newdir)
    if os.path.exists(newdir):
        if not os.path.isdir(newdir):
            raise OSError(
                "a file with the same name as the desired dir, '{0}', already exists.".format(
                    fs_decode(newdir)
                )
            )
    else:
        head, tail = os.path.split(newdir)
        # Make sure the tail doesn't point to the asame place as the head
        curdir = fs_encode(".")
        tail_and_head_match = (
            os.path.relpath(tail, start=os.path.basename(head)) == curdir
        )
        if tail and not tail_and_head_match and not os.path.isdir(newdir):
            target = os.path.join(head, tail)
            if os.path.exists(target) and os.path.isfile(target):
                raise OSError(
                    "A file with the same name as the desired dir, '{0}', already exists.".format(
                        fs_decode(newdir)
                    )
                )
            os.makedirs(os.path.join(head, tail), mode)
def mkdir_p ( newdir , mode = 0o777 ) : newdir = fs_encode ( newdir ) if os . path . exists ( newdir ) : if not os . path . isdir ( newdir ) : raise OSError ( "a file with the same name as the desired dir, '{0}', already exists." . format ( fs_decode ( newdir ) ) ) else : head , tail = os . path . split ( newdir ) curdir = fs_encode ( "." ) tail_and_head_match = ( os . path . relpath ( tail , start = os . path . basename ( head ) ) == curdir ) if tail and not tail_and_head_match and not os . path . isdir ( newdir ) : target = os . path . join ( head , tail ) if os . path . exists ( target ) and os . path . isfile ( target ) : raise OSError ( "A file with the same name as the desired dir, '{0}', already exists." . format ( fs_decode ( newdir ) ) ) os . makedirs ( os . path . join ( head , tail ) , mode )
Recursively creates the target directory and all of its parents if they do not
    already exist.  Fails silently if they do.

    :param str newdir: The directory path to ensure
    :raises: OSError if a file is encountered along the way