当前位置:首页 > Python

将print输出的内容保存到txt文件中

lonely5年前 (2021-06-24)Python1472
# 将print输出的内容保存到txt文件中

class Logger(object):
    def __init__(self, filename=os.path.join(base_path,'log/Default.log')):
        self.terminal = sys.stdout
        self.log = open(filename, "a", encoding='utf-8')

    def write(self, message):
        self.terminal.write(message)
        self.log.write(message)

    def flush(self):
        pass

sys.stdout = Logger(os.path.join(base_path,'log/main_log.txt'))


扫描二维码推送至手机访问。

版权声明:本文由复制者发布,如需转载请注明出处。

本文链接:https://www.copyer.cn/post/10.html

分享给朋友:

相关文章

推荐一个好用的Python第三方日志库loguru

推荐一个好用的Python第三方日志库loguru

        Loguru: 更优雅的日志记录解决方案…

python获取昨天的日期

python获取昨天的日期

import datetime def getYesterday():     today = datetime.d…

windows更换conda源

windows更换conda源

使用清华源一、cmd中执行conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/an…

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。