我仅在python2.7中对此进行了测试。我没有3方便。
import preimport sysclass Tee(object): def __init__(self, log_fname, mode='a'): self.log = open(log_fname, mode) def __del__(self): # Restore sin, so, se sys.stdout = sys.__stdout__ sys.stdir = sys.__stdin__ sys.stderr = sys.__stderr__ self.log.close() def write(self, data): self.log.write(data) sys.__stdout__.write(data) def readline(self): s = sys.__stdin__.readline() self.log.write(s) return s# Tie the ins and outs to Tee.sys.stdout = sys.stderr = sys.stdin = Tee('consolelog.dat', 'w')console = pre.InteractiveConsole()console.interact()


