@contextmanager
def in_tempdir():
 pwd = os.getcwd()
 d = mkdtemp()
 os.chdir(d)
 (yield d)
 os.chdir(pwd)
 rmtree(d)

@ contextmanager def in_tempdir ( ) : pwd = os . getcwd ( ) d = mkdtemp ( ) os . chdir ( d ) ( yield d ) os . chdir ( pwd ) rmtree ( d )
a decorator moving the enclosed function inside the tempfile .