它会打印到stderr
,可以从以下测试中看到:
$ cat test.py
try:
raise IOError()
except:
import traceback
traceback.print_exc()
$ python test.py
Traceback (most recent call last):
File "test.py", line 2, in <module>
raise IOError()
IOError
$ python test.py > /dev/null
Traceback (most recent call last):
File "test.py", line 2, in <module>
raise IOError()
IOError
$ python test.py 2> /dev/null
$
顺便提一下,你也可以控制它:
import traceback
import sys
try:
raise Exception
except Exception as E:
traceback.print_exc(file=sys.stderr)
with open(outFile) as fp
print_exc(fp)
print_exc
是print_exception(*sys.exc_info(), limit, file, chain)
的简写。print_exception
的文档说:它与print_tb
相同,除了<某些内容>。而print_tb
的文档则表示,默认情况下会打印到sys.stderr
。 - Op De Cirkel