from pathlib import Path


def data_to_dir(data_dir):
    """Ensures that the procided data destination is valid."""
    data_dir = Path(data_dir)
    if data_dir.is_dir():
        is_empty = not any(data_dir.iterdir())
        if is_empty:
            return data_dir
        else:
            raise FileExistsError(
                f"{data_dir.absolute()} already exists and is non-empty. "
                "Please provide a new path for data output through `-d new_dir`."
            )
    data_dir.mkdir(parents=True)
    return data_dir