Created
July 12, 2019 21:44
-
-
Save hngkr/cae19eeaa9a02f33a94d6cf4b7ea6865 to your computer and use it in GitHub Desktop.
change directory source code
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 contextlib | |
import os | |
@contextlib.contextmanager | |
def cd(path): | |
"""Set the working directory | |
Temporarily set the working directory inside the context manager and | |
reset it to the previous working directory afterwards | |
""" | |
old_dir = os.getcwd() | |
os.chdir(path) | |
try: | |
yield | |
finally: | |
os.chdir(old_dir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment