Skip to content

Instantly share code, notes, and snippets.

@orip
Created July 22, 2012 19:22
Show Gist options
  • Save orip/3160773 to your computer and use it in GitHub Desktop.
Save orip/3160773 to your computer and use it in GitHub Desktop.
temp_chdir
import os, contextlib
@contextlib.contextmanager
def temp_chdir(path):
"""
Usage:
>>> with temp_chdir(gitrepo_path):
... subprocess.call('git status')
"""
starting_directory = os.getcwd()
try:
os.chdir(path)
yield
finally:
os.chdir(starting_directory)
@mattixpet
Copy link

Hey, I want to use this code in an Apache 2.0 licensed project, can I do so with your permission?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment