How do I write a decorator that restores the cwd?
The answer for a decorator has been given; it works at the function definition stage as requested. With Python 2.5+, you also have an option to do that at the function call stage using a context manager: from __future__ import with_statement # needed for 2.5 ≤ Python < 2.6 import contextlib, os @contextlib.contextmanager def remember_cwd(): … Read more