This context manager restores the value of the current working directory (cwd) after the enclosed code block completes or raises an exception. If a directory name is supplied to the context manager then the cwd is changed prior to running the code block.
- from __future__ import with_statement # only for python 2.5
- import contextlib
- import os
- def chdir(dirname=None):
- curdir = os.getcwd()
- if dirname is not None:
You need to login to post a comment.