0
如果我有一個回溯如:如何在異常追溯中深入獲取第二項的文件名?
Traceback (most recent call last):
File "...\overall_input.py", line 5, in setup_data
MODULE1.function_name()
File "...\MODULE1.py", line 5, in function_name
value = 1/0
如何獲得第二檔深的名稱 - 即MODULE1.py
?
作爲參考,我知道可以通過捕獲異常,然後利用獲得的第一個文件(即overall_input.py
):
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
如答案Python When I catch an exception, how do I get the type, file, and line number?說明。