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

問題:

There are several ways to write to stderr: 有幾種寫stderr的方法: this

# 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? 應該使用哪一種方式? spa

There should be one — and preferably only one — obvious way to do it. 應該有一種-最好只有一種-顯而易見的方法。 .net


解決方案:

參考一: https://stackoom.com/question/NOEY/如何在Python中打印到stderr
參考二: https://oldbug.net/q/NOEY/How-to-print-to-stderr-in-Python
相關文章
相關標籤/搜索