如何输出到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的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python3如何使用pil

啥是佩奇,Python 告诉你!

flask 和 django 区别

Python如何将数据导出excel的技巧分享

Python 杨辉三角方法的实例介绍

Python中time的基本介绍

使用Python操作excel文件

Python2和3区别大吗

Python中线程与进程的区别与优劣

Python如何模拟实现生产者消费者模式的代码案例

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




打赏

取消

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

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

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

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

评论

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