Python capture all printed output

前提是你 提交于 2019-12-04 01:52:18

You can do this by setting sys.stdout to be a file of your choice

import sys

sys.stdout = open('out.dat', 'w')
print "Hello"

sys.stdout.close()

Will not display any output but will create a file called out.dat with the printed text.

Note that this doesn't need to be an actual file but could be a StringIO instance, which you can just use the getvalue method of to access everything that has been printed previously.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!