Skip to content

Instantly share code, notes, and snippets.

@abingham
Created March 22, 2021 08:38
Show Gist options
  • Save abingham/2e10cf6cf57e0285f384414fa125e692 to your computer and use it in GitHub Desktop.
Save abingham/2e10cf6cf57e0285f384414fa125e692 to your computer and use it in GitHub Desktop.
Find dominating file name
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