Skip to content

Instantly share code, notes, and snippets.

@pletnes
Created May 25, 2020 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pletnes/f50acc4f6658ffbd8ad84a5aaf002ae2 to your computer and use it in GitHub Desktop.
Save pletnes/f50acc4f6658ffbd8ad84a5aaf002ae2 to your computer and use it in GitHub Desktop.
Contextmanager `cd` to enter/leave directory
@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