from pathlib import Path


def find_confounds_file(nii_file):
    """Finds the corresponding confounds.tsv file for a bold.nii.gz

    Parameters:
        nii_file: pathlib.Path

    Returns:
        confounds_file: pathlib.Path
    """
    confounds_options = [str(fname).replace("desc-confounds_timeseries.tsv", "") for
                         fname in nii_file.parent.glob("*confound*tsv")]
    confounds_file, = [fname for fname in confounds_options if
                       str(nii_file).startswith(fname)]
    return Path(confounds_file + "desc-confounds_timeseries.tsv")