python 将print输出的内容保存到txt文件中
时间:2020-10-21 21:02:09|栏目:Python代码|点击: 次
具体代码如下所示:
import sys
import os
class Logger(object):
def __init__(self, filename="Default.log"):
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('a.txt')
print(path)
print(os.path.dirname(__file__))
print('------------------')
总结
上一篇:Python命令行参数解析模块optparse使用实例
栏 目:Python代码
下一篇:pandas创建新Dataframe并添加多行的实例
本文标题:python 将print输出的内容保存到txt文件中
本文地址:http://www.codeinn.net/misctech/14541.html






