设为首页 加入收藏

TOP

Python教程:sys.stdout方法(二)
2023-09-23 15:44:25 】 浏览:603
Tags:Python 教程 sys.stdout 方法
def g(*a, **kw): for f in self._myfiles: res = getattr(f, attr, *arguments)(*a, **kw) return res return g sys.stdout = multipleSave([ sys.stdout, open('file.txt', 'w') ]) # Python小白学习交流群:711312441 # all print statement works here print ('123') print (sys.stdout, 'this is second line') sys.stdout.write('this is third linen')

输出:

# file.txt will be created on the same directory with multiple logs in it.
123
<__main__.multipleSave object at 0x00000226811A0048> this is second line
this is third line

为了将输出的控制台结果存储在一个文件中,我们可以使用open() 方法来存储它。我们将所有的控制台输出存储在同一个日志文件中。

这样,我们可以存储任何打印到控制台的输出,并将其保存到日志文件中。

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇 一条爬虫抓取一个小网站所有数据 下一篇Python教程(13)——Python运算符..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目