Sunday, July 22, 2012

Python: change and restore the working directory

Occasionally a command should be run from a specific directory. Here's a Python helper for this:

  • try/finally ensures the original working directory is restored.
  • contextlib.contextmanager is a neat way to write things supporting the "with" statement ("Context Managers").