Last active
March 14, 2019 10:29
-
-
Save Tadaboody/e18ebe50daedd7cac223979f22d33451 to your computer and use it in GitHub Desktop.
Cd as a context manager
This file contains 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
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