Created
March 22, 2021 08:38
-
-
Save abingham/2e10cf6cf57e0285f384414fa125e692 to your computer and use it in GitHub Desktop.
Find dominating file name
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
def find_dominating(filename: str, start_path: Path): | |
"""Look for `filename` in `start_path` or any of its ancestor directories. | |
Args: | |
filename: The filename to look for | |
start_path: The directory in which to start the search | |
Returns: The dominating path | |
Raises: | |
FileNotFoundError: The dominating directory is not found. | |
""" | |
path = start_path.absolute() | |
if path.is_file(): | |
path = path.parent | |
while True: | |
if (path / filename).exists(): | |
return path | |
if path == path.parent: | |
break | |
path = path.parent | |
raise FileNotFoundError( | |
f'No file named "{filename}" dominating {start_path}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment