Python同时输出到屏幕和文件(Logger)

import sys
import os
 
class Logger(object):
    def __init__(self, filename="log.txt"):
        self.terminal = sys.stdout
        self.log = open(filename, "a")
 
    def write(self, message):
        self.terminal.write(message)
        self.log.write(message)
        self.log.flush()    #缓冲区的内容及时更新到log文件中
    
    def flush(self):
        pass

path = os.path.abspath(os.path.dirname(__file__))
type = sys.getfilesystemencoding()
sys.stdout = Logger()

#之后用print输出的就既在屏幕上,又在log文件里
print(453453)
print(path)
print(type)

def flush  不能省

猜你喜欢

转载自blog.csdn.net/hxxjxw/article/details/120752661
今日推荐