python - AttributeError:“字节”对象没有属性“告诉”
问题描述
我正在使用email.message
并smtplib
使用 python 发送电子邮件。当图像作为附件发送时,会引发以下错误:
AttributeError: 'bytes' object has no attribute 'tell'
这是图像附件的代码:
if filetype.lower() in ['jpg','jpeg','png','gif']:
with open(filename, 'rb') as file:
file_data = file.read()
image_type = imghdr.what(file_data)
actual_filename = filename.split('/')[-1]
msg.add_attachment(file_data, maintype='image', subtype=image_type, filename=actual_filename)
解决方案
宁愿做
with open(filename, 'rb') as file:
file_data = file.read()
image_type = imghdr.what(file_data)
你可能会
image_type = imghdr.what(filename)
with open(filename, 'rb') as file:
file_data = file.read()
测试 file 命名的文件中包含的图像数据,并返回描述图像类型的字符串。如果提供了可选的 h,则忽略 file 参数并假定 h 包含要测试的字节流。
推荐阅读
- html - 如何创建一个固定的 div 和第二个可滚动的?
- c# - 使用 C# Array.ConvertAll 时,Visual Studio 2017 调试器不显示变量
- r - 如何在 R 中的数据帧上应用 CNN?
- spring-boot - 从源码讨论SpringApplication的run方法是否会执行两次
- python - 静态文件问题,除了一些文件 style.css 和 js 外,所有内容都已加载
- ruby - 2020 年的 Ruby 2.2.3
- paypal - 对第三方的大规模支付建议
- python - 如何在 Tkinter 的窗口中更改具有图形的子图参数?例如我想添加 xlabel 和 ylabel
- c++11 - 在 C++ 中使用 2D 数组指针复制结构 - 指针算术
- java - 使用 SaxParser 修改 xml 属性