打印标准输出,同时也打印到Python中的文本文件
有没有什么方法可以在打印标准输出的同时打印到文本文件?例如:打印标准输出,同时也打印到Python中的文本文件
import sys
sys.stdout = open('file.txt', 'w')
#The stuff below should be printed out and at the same time being printed to a text file
print "This is in a text file and stdout"
所以,再次,有没有办法做到这一点?或者不能这样做?
如果这是在脚本中,请查看unix命令tee
。这将允许您将stdout
分为两个方向,一个转到一个文件,另一个只是继续转到stdout
(我是一个python newb)例子? –
@TheNotGoodAtCodeGuy'python myscript | tee mylog.txt'将运行一个名为'myscript.py'的脚本,将'stdout'写入'mylog.txt',并将'stdout'输出到您的控制台 – Stankalank
@TheNotGoodAtCodeGuy不会忘记接受答案=] – Stankalank
'tee' - http://stackoverflow.com/questions/616645/how-doi-i- python – Scott
你需要一个多平台解决方案吗? – Matt