本文摘自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的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
caffe导入到Python报错怎么办
Python如何批量修改文件后缀名?批量修改文件后缀名的方法
Python环境下安装mysqldb模块
Python怎么安装库
详解Python中super()函数的用法及工作原理
Python中数据结构与算法的应用(附示例)
Python中的排序操作和heapq模块的介绍(代码示例)
Python如何实现从pdf文件中爬取表格数据(代码示例)
string在Python中是什么意思
Python轻松实现图片旋转
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » 如何输出到stderr