1 2 3 4 5 6 7 8 9 10 11 12 13 14
import contextlib import os import pathlib from typing import Iterator @contextlib.contextmanager def as_cwd(path: pathlib.Path) -> Iterator[pathlib.Path]: old_cwd = os.getcwd() os.chdir(path) try: yield path finally: os.chdir(old_cwd)