Skip to content

Instantly share code, notes, and snippets.

@hngkr
Created July 12, 2019 21:44
Show Gist options
  • Save hngkr/cae19eeaa9a02f33a94d6cf4b7ea6865 to your computer and use it in GitHub Desktop.
Save hngkr/cae19eeaa9a02f33a94d6cf4b7ea6865 to your computer and use it in GitHub Desktop.
change directory source code
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