Skip to content

Instantly share code, notes, and snippets.

@hngkr hngkr/cd.py
Created Jul 12, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.