Created
May 25, 2020 08:03
-
-
Save pletnes/f50acc4f6658ffbd8ad84a5aaf002ae2 to your computer and use it in GitHub Desktop.
Contextmanager `cd` to enter/leave directory
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
@contextlib.contextmanager | |
def cd(directory): | |
pre_directory = os.getcwd() | |
os.chdir(str(directory)) | |
try: | |
yield | |
finally: | |
os.chdir(str(pre_directory)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment