Home > Article > Backend Development > How to output to stderr
sys.stderr The purpose is to return error information
For example:
shutddown will prompt an error in red font.
>>> shutddown Traceback (most recent call last): File "", line 1, in shutddown NameError: name 'shutddown' is not defined
You can think about it, when we customize a function and then need to prompt the user for an error, but obviously it prints out in green font, then sys.stderr comes into play.
>>> 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 >>>
or as follows:
>>> sys.stderr.write('sf') sf >>> print sys.stderr.write('sf') sfNone
Related free learning recommendations: python video tutorial
The above is the detailed content of How to output to stderr. For more information, please follow other related articles on the PHP Chinese website!