from pathlib import Path
import re


def find_target_file (wdir, stem, extension, no_hit_ok=True, recursive=False):
    """
    Find a target file path from a path to the working directory, the stem of the file name, and the extension of the target file.
    """
    if recursive:
        paths = list(Path(wdir).glob('**/{}*'.format(stem)))
    else:
        paths = list(Path(wdir).glob('{}*'.format(stem)))
    path = [ i for i in paths if re.search(extension, str(i)) ]
    if len(path)==0:
        if no_hit_ok:
            ret = ''
        else:
            raise ValueError('No file is matched.')
    elif len(path)>1:
        raise ValueError('More than one file is matched.')
    else:
        ret = str(path[0])
    return ret