如何在Python中打印到stderr? - How to print to stderr in Python?

08/12 14:10
阅读数 45

问题:

There are several ways to write to stderr: 有几种写stderr的方法:

# Note: this first one does not work in Python 3
print >> sys.stderr, "spam"

sys.stderr.write("spam\n")

os.write(2, b"spam\n")

from __future__ import print_function
print("spam", file=sys.stderr)

That seems to contradict zen of Python #13 , so what's the difference here and are there any advantages or disadvantages to one way or the other? 这似乎与zen的Python#13 相矛盾,所以这里有什么区别,一种方式或另一种方式有什么优点或缺点? Which way should be used? 应该使用哪种方式?

There should be one — and preferably only one — obvious way to do it. 应该有一种-最好只有一种-显而易见的方法。


解决方案:

参考一: https://stackoom.com/question/NOEY/如何在Python中打印到stderr
参考二: https://oldbug.net/q/NOEY/How-to-print-to-stderr-in-Python
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
在线直播报名
返回顶部
顶部