@contextlib.contextmanager
def work_in(dirname=None):
 curdir = os.getcwd()
 try:
  if (dirname is not None):
   os.chdir(dirname)
  (yield)
 finally:
  os.chdir(curdir)

@ contextlib . contextmanager def work_in ( dirname = None ) : curdir = os . getcwd ( ) try : if ( dirname is not None ) : os . chdir ( dirname ) ( yield ) finally : os . chdir ( curdir )
context manager version of os .