The Traceback Module
The traceback module lets you log or display exception tracebacks programmatically.
The traceback module lets you log or display exception tracebacks programmatically.
import traceback
try:
1 / 0
except ZeroDivisionError:
traceback.print_exc()
# or: text = traceback.format_exc()
Use traceback.format_exc() to log errors to a file.