Skip to content

Instantly share code, notes, and snippets.

@Tadaboody
Last active March 14, 2019 10:29
Show Gist options
  • Save Tadaboody/e18ebe50daedd7cac223979f22d33451 to your computer and use it in GitHub Desktop.
Save Tadaboody/e18ebe50daedd7cac223979f22d33451 to your computer and use it in GitHub Desktop.
Cd as a context manager
import os
from contextlib import contextmanager
from typing import Union, ContextManager
PathType = Union[os.PathLike, str]
@contextmanager
def cd(path: PathType) -> ContextManager[None]:
old_path = Path.cwd()
os.chdir(path)
yield
os.chdir(old_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment