如何输出到stderr


本文摘自php中文网,作者anonymity,侵删。

sys.stderr 目的就是返回错误信息

比如:

shutddown会红色字体提示错误。

1

2

3

4

5

>>> shutddown

Traceback (most recent call last):

  File "", line 1, in

    shutddown

NameError: name 'shutddown' is not defined

可以想一下,当我们自定义一个函数,然后需要提示用户错误的时候,但显然print出来是绿色字体,这时候sys.stderr就有作用了。

1

2

3

4

5

6

7

8

9

10

11

12

13

>>> 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

>>>

或者如下:

1

2

3

4

>>> sys.stderr.write('sf')

sf

>>> print sys.stderr.write('sf')

sfNone

相关免费学习推荐:python视频教程

以上就是如何输出到stderr的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python爬虫可以自学吗

Python是什么软件?

Python怎么输入数字

Python的for循环怎么理解

Python怎样在excel中应用?

int在Python中什么意思

Python中函数的全面总结(附实例)

Python获取昨天、今天、明天开始、结束时间戳的方法

Python中logging的详细介绍(附示例)

Python哪年出来的

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...