python - 如何在python中将回溯消息打印到新行
问题描述
我正在使用 html 电子邮件和 .format() 作为字符串传递参数
下面是我的python代码:
import sys
try:
print(5 / 0)
except Exception as e:
send_error_email(exp_message=format_exc())
然后获取函数 send_error_email 并传递给 MIMEMultipart 邮件脚本
html = """
<html>
<body>
<b> Exception Details: </b> {exp_message}
</body>
</html>
""".format(exp_message=exp_message)
在邮件中的一行中获取输出:
异常数据:回溯(最近一次调用最后一次):文件“C:\Build\test\workfile\python-practice\exception_test.py”,第 54 行,在 get_details print(100/0) ZeroDivisionError:除以零
预期输出应该是新行中的每条消息:
异常数据:回溯(最近一次调用):
文件“C:\Build\test\workfile\python-practice\exception_test.py”,第 54 行,
在 get_details print(100/0)
ZeroDivisionError:整数除法或模除以零
解决方案
为了更好和漂亮地打印异常,请具体参考Python 的 Traceback 库
,检查 traceback.format_* 方法(例如 format_exception())
推荐阅读
- python - 简单的for循环将系列合并到数据帧上的内存错误
- python - 熊猫 - 日期分组内的分箱
- java - NetBeans 8.2 看不到创建 Java 应用程序的选项
- webpack - webpack 仅编译,没有构建输出以检查退出代码
- reactjs - 更改使用 chrome 扩展做出反应的输入值
- c# - 具有统一 BorderThickness 的边框无法绑定 BorderBrush 颜色
- css - 为什么在使用响应式桌面、移动设备或平板电脑时,在 hr 上使用 margin left = 47% 会有不同的作用?
- javascript - 下载网站清单需要执行哪些必要步骤?
- google-chrome - 屏幕阅读器无法读出移动浏览器上的复选框标签
- go - 在 golang 中更新 grpc 的接收和发送消息大小