from pathlib import Path


def the_path_that_matches(pattern: str, in_directory):
    """
    Finds one and only one path matching the specified pattern. Raises an error if it finds 2+ paths or no paths.

    To learn how to use advanced patterns, read http://www.robelle.com/smugbook/wildcard.html

    Parameters
    ----------
    pattern : str
        Pattern to search for.
    in_directory : str or Path
        Directory in which to search for the pattern.

    Returns
    -------
    Path
        Path found by search.

    Raises
    ------
    IOError
        If it finds 2+ paths or no paths.
    """

    matches = list(Path(in_directory).glob(pattern))

    if not Path(in_directory).is_dir():
        raise IOError(f"{in_directory} either doesn't exist or isn't a directory at all!")

    elif(len(matches)) >= 2:
        raise IOError(f"The directory {in_directory} exists but contains more than one path that matches '{pattern}': {[match.name for match in matches]}")

    elif(len(matches)) == 0:
        raise IOError(f"The directory {in_directory} exists but contains no paths that match pattern '{pattern}'")
    
    else:
        return matches[0]