在python中,我可以将打印函数的输出重定向到stderr吗? [英] In python, can I redirect the output of print function to stderr?
本文介绍了在python中,我可以将打印函数的输出重定向到stderr吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的程序中有很多 print
函数(python 2.7
).有什么办法可以添加几行然后所有输出都可以重定向到stderr
?我想要的是python代码而不是linux管道.
There're lots of print
function (python 2.7
) in my program. Is there any way I can add a few lines then all the output can be redirected to stderr
? What I want is python codes but not linux pipeline.
比如我的程序是这样的:
For example, my program is like:
print 'hello world'
我想添加一些代码,例如:
I would like to add some codes like:
redirect_output_to_stderr()
print 'hello world'
然后所有的输出都可以重定向到stderr
.
Then all the output can be redirected to stderr
.
我知道 print >>sys.stderr, 'hello world'
可以实现我的目标,但是有什么办法可以防止修改现有代码吗?
I know print >> sys.stderr, 'hello world'
can achieve my goal, but is it any way that can prevent from modifying the existed codes?
推荐答案
在你的方法中这样做:
import sys
sys.stdout = sys.stderr
这篇关于在python中,我可以将打印函数的输出重定向到stderr吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文