Ensure the existence of the (parent) directory of f

def ensure_dir_exists(f, fullpath=False):
    """
    Ensure the existence of the (parent) directory of f
    """
    if fullpath is False:
        # Get parent directory
        d = os.path.dirname(f)
    else:
        # Create the full path
        d = f
    if not os.path.exists(d):
        os.makedirs(d)