@contextmanager
def in_dir(dir=None):
 cwd = os.getcwd()
 if (dir is None):
  (yield cwd)
  return
 os.chdir(dir)
 (yield dir)
 os.chdir(cwd)

@ contextmanager def in_dir ( dir = None ) : cwd = os . getcwd ( ) if ( dir is None ) : ( yield cwd ) return os . chdir ( dir ) ( yield dir ) os . chdir ( cwd )
change directory to given directory for duration of with block useful when you want to use in_tempdir for the final test .