我最近開始使用Python進行開發,並且我注意到無論何時我的代碼生成運行時錯誤,細節都會在輸出中閃爍一秒,然後程序關閉。有沒有辦法在發生這種情況時停止程序關閉或將錯誤記錄到文本文件中?Python錯誤報告
0
A
回答
1
除了評論中的所有工作都很好的建議之外,您始終可以(也應該始終)使用logging
模塊。您可以快速設置基本日誌記錄功能,並允許更改顯示給用戶的信息級別。
(從'Python Module of The Week' website)
import logging
LOG_FILENAME = 'logging_example.out'
logging.basicConfig(filename=LOG_FILENAME,
level=logging.DEBUG)
logging.debug('This message should go to the log file')
f = open(LOG_FILENAME, 'rt')
try:
body = f.read()
finally:
f.close()
print 'FILE:'
print body
您可以登錄到不同層次(即信息,警告,調試誤差),並將這些顯示在代碼運行時,或隱藏他們,通過設置水平參數logging.basicConfig()
。
+0
當然,對於幾行腳本使用日誌記錄模塊看起來可能過分;它仍然可以工作,但如果你只是原型設計,那麼用'>'將其轉儲到文件中可能會更好地滿足你的需求。對於模塊和程序,'logging'是必須的。 – nlsdfnbch
1
相關問題
- 1. Python OSError不報告錯誤
- 2. aptana報告python類型錯誤
- 3. GDK錯誤報告
- 4. Laravel報告錯誤
- 5. XAMPP錯誤報告
- 6. MongoDB錯誤報告
- 7. PHP報告錯誤
- 8. GSL:錯誤報告
- 9. Bash錯誤報告
- 10. SEH錯誤報告
- 11. jasper報告錯誤狀態錯誤
- 12. MVC ModelState錯誤地報告錯誤
- 13. React JS錯誤的錯誤行報告
- 14. 「錯誤報告WTF」在logcat的錯誤
- 15. 僅報告一次錯誤
- 16. SQL報告服務錯誤
- 17. JMeter總結報告%錯誤
- 18. PHP按需錯誤報告
- 19. sizeof報告錯誤的值
- 20. PHP錯誤報告關閉
- 21. PHP錯誤報告:Wordpress
- 22. #rdlc報告中的錯誤
- 23. CodeIgniter on 1and1錯誤報告
- 24. C#RDLC報告錯誤
- 25. SQL觸發錯誤報告
- 26. Dataflow的錯誤報告
- 27. perl6語法,錯誤報告
- 28. 亂舞錯誤報告
- 29. 我jquery.cookie.js報告錯誤
- 30. Boost.Spirit:Lex + Qi錯誤報告
運行從'蟒蛇filename.py'命令行Python程序。 –
用'python filename.py> output.txt'記錄錯誤。 –
或者使用任何Python IDE,例如https://www.jetbrains.com/pycharm/ – bastelflp