当前位置:首页 > Python > 正文内容

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

lonely3年前 (2021-06-24)Python588
# 将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

分享给朋友:

相关文章

Robot Framework安装教程

Robot Framework安装教程

    Robot Framework是一个基于Python的,可扩展的关键字驱动的测试自动化框架,用于端到端验收测试和验收测试驱动开发(ATDD)。它可用于测试分布式异...

python-selenium设置webdriver隐藏浏览器运行及不加载图片

python-selenium设置webdriver隐藏浏览器运行及不加载图片

from selenium import webdriver # 设置无界面 opt = webdriver.ChromeOpt...

详解python的xlwings库读写excel操作总结

详解python的xlwings库读写excel操作总结

 一、总结一句话总结:xlwings 是 Python 中操作Excel的一个第三方库,支持.xls读写,.xlsx读写,操作非常简单,功能也很强大1、xlwings 中的逻辑:应用->...

Python pip更换安装源

Python pip更换安装源

常见的镜像源:华为:https://repo.huaweicloud.com/repository/pypi/simple 清华:https://pypi.tuna.tsinghua.edu...

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

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

        Loguru: 更优雅的日志记录解决方案loguru 是一个 Python 简易且强大的第三方日志记录库...

python获取昨天的日期

python获取昨天的日期

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

发表评论

访客

看不清,换一张

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