比方:
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的细致内容,更多请关注ki4网别的相干文章!