[Python-ideas] chdir context manager

Paul Moore p.f.moore at gmail.com
Sat Jan 19 12:15:14 CET 2013


On 19 January 2013 10:10, Daniel Shahaf <d.s at daniel.shahaf.name> wrote:
> The following is a common pattern (used by, for example,
> shutil.make_archive):
>
>     save_cwd = os.getcwd()
>     try:
>         foo()
>     finally:
>         os.chdir(save_cwd)
>
> I suggest this deserves a context manager:
>
>     with saved_cwd():
>         foo()

+1. I've written this myself many times...

Paul



More information about the Python-ideas mailing list