create a directory if required

def ensure_dir_exists(path):
    """ create a directory if required """
    dir_path = os.path.dirname(path)
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)