Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python文件附件错误_Python_Python 3.x - Fatal编程技术网

Python文件附件错误

Python文件附件错误,python,python-3.x,Python,Python 3.x,我试图在python中添加电子邮件附件,但不断出现错误: IsADirectoryError:[Errno 21]是一个目录:'/Users/myname/Desktop/Current Desktop/Folder' 这是我的密码: file = "myfile.pdf" attachment = open("/Users/myname/Desktop/Current Desktop/Folder","rb") part = MIMEBase('application', 'octet-s

我试图在python中添加电子邮件附件,但不断出现错误:

IsADirectoryError:[Errno 21]是一个目录:'/Users/myname/Desktop/Current Desktop/Folder'

这是我的密码:

file =  "myfile.pdf"
attachment = open("/Users/myname/Desktop/Current Desktop/Folder","rb")

part = MIMEBase('application', 'octet-stream')
part.set_payload((file).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename=%s" % file)

谢谢你的帮助

我想你想要的是

with open("/Users/myname/Desktop/Current Desktop/Folder/" + "myfile.pdf", "rb") as file: 

    part = MIMEBase('application', 'octet-stream')
    part.set_payload(file.read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', "attachment; filename=%s" % file)

我想你想要像这样的东西

with open("/Users/myname/Desktop/Current Desktop/Folder/" + "myfile.pdf", "rb") as file: 

    part = MIMEBase('application', 'octet-stream')
    part.set_payload(file.read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', "attachment; filename=%s" % file)

/Users/myname/Desktop/Current Desktop/Folder
是目录吗?看起来是…看起来你正试图附加一个目录。您可能需要先对其进行压缩或压缩,然后再附加..它是一个包含我试图附加的文件-myfile.pdf-的目录。我试图为附件写入文件路径。我想我不明白什么。是目录吗?看起来是…看起来你正试图附加一个目录。您可能需要先对其进行压缩或压缩,然后再附加..它是一个包含我试图附加的文件-myfile.pdf-的目录。我试图为附件写入文件路径。我想我不明白什么。这很有效,非常感谢!唯一需要更改的是,rb需要用引号括起来,比如“rb”。除此之外,它工作得完美无缺!我会在3分钟内接受答案,没问题!容易解决。再次感谢!这非常有效,非常感谢!唯一需要更改的是,rb需要用引号括起来,比如“rb”。除此之外,它工作得完美无缺!我会在3分钟内接受答案,没问题!容易解决。再次感谢!