当前位置:首页 > Python

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

lonely5年前 (2021-06-24)Python1604
# 将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-selenium设置webdriver隐藏浏览器运行及不加载图片

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

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

返回当前时间及格式化已使用时间

返回当前时间及格式化已使用时间

import time import datetime # 格式化输出使用时间 class Farmat_time():   &…

python获取昨天的日期

python获取昨天的日期

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

PyQt5安装

PyQt5安装

下面直接使用pip来安装PyQt5,此处可能是pip/pip3,或者两者皆可,后面不再重复直接pip安装PyQt5pip install PyQt5由于Qt Designer已经在…

conda常用基本命令

conda常用基本命令

1、查看所有虚拟环境conda env list2、创建虚拟环境…

发表评论

访客

看不清,换一张

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