我正在编写一个脚本来从树莓派 4 发送电子邮件,我想通过 python 将文件附加到电子邮件,但我总是收到此错误。
我对Python还很陌生。我知道该文件位于正确的位置,并且我尝试了其他一些文件。我尝试了文件的完整路径。我读了很多其他类似的问题,但没有运气
filename = '123.avi'
attachmet = open(filename,'rb')
part = MIMEBase('application','octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment; filename= "+filename)
msg.attach(part)
如果没有这行脚本,脚本可以正常工作。 我收到邮件了。
但是有了这行我得到错误:
Traceback (most recent call last):
File "/home/pi/Monitor/sendmail.py", line 31, in <module>
attachmet = open(filename,'rb')
IOError: [Errno 2] No such file or directory: '123.avi'
最佳答案
这对我有用。
with open(full_path_of_file, "rb") as fs:
part = MIMEBase('application', "octet-stream")
part.set_payload(fs.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(full_path_of_file))
msg.attach(part)
关于python - IO错误: [Errno 2] No such file or directory: '123.avi' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58524981/