Creating a directory and its parents in Python

You can use the os.makedirs() function to create a new directory and any intermediate directories in its path that don’t exist:

import os
path = '/tmp/spam/eggs/ham'
if os.path.isdir(path):
    print 'Directory created'

The reverse operation of deleting a directory and all its subdirectories can be performed with shutil.rmtree():

import shutil