Python print输出重定向到文件和屏幕,超简单


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)
 
    def flush(self):
        pass

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





微信公众号:AutoML机器学习
MARSGGBO原创
如有意合作或学术讨论欢迎私戳联系~
邮箱:marsggbo@foxmail.com

2020.03.1



posted @ 2019-01-19 22:18  marsggbo  阅读(12232)  评论(2编辑  收藏  举报