IOError: [errno 2] 没有这样的文件或目录: '123.avi'

  • 本文关键字:文件 avi errno IOError python
  • 更新时间 :
  • 英文 :


我正在编写一个脚本来从树莓派 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)

最新更新