@contextlib.contextmanager
def working_directory(directory=None, filename=None):
 assert (bool(directory) != bool(filename))
 if (not directory):
  directory = os.path.dirname(filename)
 prev_cwd = os.getcwd()
 os.chdir(directory)
 try:
  (yield)
 finally:
  os.chdir(prev_cwd)

@ contextlib . contextmanager def working_directory ( directory = None , filename = None ) : assert ( bool ( directory ) != bool ( filename ) ) if ( not directory ) : directory = os . path . dirname ( filename ) prev_cwd = os . getcwd ( ) os . chdir ( directory ) try : ( yield ) finally : os . chdir ( prev_cwd )
a context manager which changes the working directory to the given path .