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

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

lonely3年前 (2021-06-24)Python484
# 将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获取基础路径供其他模块导入

python获取基础路径供其他模块导入

# 文件1:获取基础路径,供其他文件导入,当前文件是conf/settings.py文件 import os base_path = os.path.d...

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

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

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

python使用国内镜像源安装更新模块

python使用国内镜像源安装更新模块

由于python更新源速度较慢,且经常更新失败。国内更新源可秒更,以下是配置方法。1、python2.7更新pip命令1python -m pip install --upgrad...

windows更换conda源

windows更换conda源

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

SQLAlchemy中使用sqlacodegen自动同步数据库中表生成model

SQLAlchemy中使用sqlacodegen自动同步数据库中表生成model

前言如果数据库中的表已经存在了,我们只想通过 SQLAlchemy 操作数据库表的数据,不需要建表。这时可以不用一个个声明每个字段类型,可以用第三方包 sqlacodegen 自动...

conda 删除虚拟环境报错CondaEnvironmentError: cannot remove current environment

conda 删除虚拟环境报错CondaEnvironmentError: cannot remove current environment

conda 删除虚拟环境报错CondaEnvironmentError: cannot remove current environment. deactivate and run conda rem...

发表评论

访客

看不清,换一张

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