1
將sys.stderr重定向到IPython和控制檯(Gnome終端)中的文本文件會得到不同的結果。在IPython和控制檯中重定向sys.stderr的不一致性
f=open('std.log','w')
sys.stderr=f
raise Exception,"message goes here"
在IPython中,錯誤消息直接打印在屏幕上。
In [13]: run std.py
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
/home/xiaohan/code/diving-in-python/htmlparser/std.py in <module>()
2 f=open('std.log','w')
3 sys.stderr=f
----> 4 raise Exception,"message goes here"
5
6
Exception: message goes here
WARNING: Failure executing file: <std.py>
但是,如果我直接運行它的控制檯。
python std.py
錯誤消息被隱藏並重定向到文本文件。
關於這裏發生了什麼的任何建議?