Skip to content

Instantly share code, notes, and snippets.

@olemb
Created August 23, 2011 05:28
Show Gist options
  • Save olemb/1164415 to your computer and use it in GitHub Desktop.
Save olemb/1164415 to your computer and use it in GitHub Desktop.
chdir context handler
import os
from contextlib import contextmanager
@contextmanager
def chdir(dirname):
old = os.getcwd()
os.chdir(dirname)
yield old
os.chdir(old)
# Example of use
with chdir('/tmp'):
print(os.getcwd())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment