Skip to content

Instantly share code, notes, and snippets.

@Tadaboody
Created October 29, 2018 12:59
Show Gist options
  • Save Tadaboody/978a4c6dcb073c7f02742a1357375cb8 to your computer and use it in GitHub Desktop.
Save Tadaboody/978a4c6dcb073c7f02742a1357375cb8 to your computer and use it in GitHub Desktop.
Context manager to cd into a dir for a while
import os
from contextlib import contextmanager
@contextmanager
def cd(path: os.PathLike):
"""Context manager that sets the cwd to be `path`"""
old_path = os.getcwd()
os.chdir(path)
yield
os.chdir(old_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment