Last active
November 21, 2016 23:19
-
-
Save dmyersturnbull/bb1b2c1560b6ee77a959e7008c4baa85 to your computer and use it in GitHub Desktop.
Find a unique file in a directory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Douglas Myers-Turnbull wrote this for the Kokel Lab, which has released it under the Apache Software License, Version 2.0 | |
| # See the license file here: https://gist.github.com/dmyersturnbull/bfa1c3371e7449db553aaa1e7cd3cac1 | |
| # The list of copyright owners is unknown | |
| from .scan_for_files import scan_for_files # see https://gist.github.com/dmyersturnbull/80845ba9ebab2da83963 | |
| from typing import Callable, Iterator | |
| def find_only_file_matching(directory: str, matcher: Callable[[str], bool], file_iterator: Callable[[str], Iterator[str]]=scan_for_files) -> str: | |
| """Returns the full path of the matching file and raises an exception if none are found or more than 1 is found.""" | |
| file = None | |
| for f in file_iterator(directory): | |
| if matcher(f): | |
| if file is not None: | |
| raise ValueError("Multiple matching files found in {}".format(directory)) | |
| file = f | |
| if file is None: | |
| raise ValueError("No matching file found in {}".format(directory)) | |
| return file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment