把python在cmd的打印实时保存到log文件
from ctypes import *
import sys
class Logger(object):
def __init__(self, filename='default.log', stream=sys.stdout):
self.terminal = stream
self.log = open(filename, 'w')
def write(self, message):
self.terminal.write(message)
self.terminal.flush()
self.log.write(message)
self.log.flush()
def flush(self):
pass
sys.stdout = Logger("tmp.log", sys.stdout)
sys.stderr = Logger("tmp.log_file", sys.stderr)
print("this is a test!")