时间:2021-07-01 10:21:17 帮助过:649人阅读

比如:
shutddown会红色字体提示错误。
>>> shutddown
Traceback (most recent call last):
File "", line 1, in
shutddown
NameError: name 'shutddown' is not defined可以想一下,当我们自定义一个函数,然后需要提示用户错误的时候,但显然print出来是绿色字体,这时候sys.stderr就有作用了。
>>> import sys
>>> print 'closing...'
closing...
>>> print >> sys.stderr,'closing...'
closing...
>>> def test(x):
if x == 0: print >> sys.stderr,'Error--> x:can\'t is zero'
else:print x
>>> test('what\'s')
what's
>>> test(0)
Error--> x:can't is zero
>>>或者如下:
>>> sys.stderr.write('sf')
sf
>>> print sys.stderr.write('sf')
sfNone以上就是如何输出到stderr的详细内容,更多请关注Gxl网其它相关文章!