Home >Backend Development >Python Tutorial >How to output to stderr

How to output to stderr

anonymity
anonymityOriginal
2019-05-25 13:48:382721browse

sys.stderr The purpose is to return error information

How to output to stderr

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn