Façon efficace de créer des chemins récursifs Python

J'ai besoin d'une fonction simple pour créer un chemin d'accès en Python où le parent peut ou peut ne pas exister.

De la documentation python os.makedirs échouera si l'un des parents existe.

J'ai écrit ci-dessous la méthode qui fonctionne en fait comme de nombreux sous-répertoires nécessaires.

Ne ce look efficace?

def create_path(path):
    import os.path as os_path
    paths_to_create = []
    while not os_path.lexists(path):
        paths_to_create.insert(0, path)
        head,tail = os_path.split(path)
        if len(tail.strip())==0: # Just incase path ends with a /or \
            path = head
            head,tail = os_path.split(path)
        path = head

    for path in paths_to_create:
        os.mkdir(path)

source d'informationauteur |