【发布时间】:2015-06-14 10:06:44
【问题描述】:
我正在尝试使用我的邮件和 python 发送 png 图像。 这是我找到的一个脚本:
# Import smtplib for the actual sending function
import smtplib
# Here are the email package modules we'll need
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
COMMASPACE = ', '
# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = 'Our family reunion'
# me == the sender's email address
# family = the list of all recipients' email addresses
msg['From'] = me
msg['To'] = COMMASPACE.join(family)
msg.preamble = 'Our family reunion'
# Assume we know that the image files are all in PNG format
for file in pngfiles:
# Open the files in binary mode. Let the MIMEImage class automatically
# guess the specific image type.
with open(file, 'rb') as fp:
img = MIMEImage(fp.read())
msg.attach(img)
# Send the email via our own SMTP server.
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()
来源:https://docs.python.org/3/library/email-examples.html
我的问题是,当我想要精确的 pngfiles 路径时,我会写如下内容:
pngfiles="/Desktop/Test2"
只返回这样的错误信息
Traceback (most recent call last):
File "C:\Python27\work\Picture.py", line 46, in <module>
fp = open(file, 'rb')
IOError: [Errno 13] Permission denied: '/'
这确实是一个愚蠢的问题,但不知道如何正确编写它...有什么帮助吗? :)
谢谢!
【问题讨论】:
-
尝试从路径中删除前导斜杠:
pngfiles = "Desktop/Test2"。 -
尝试但重新输入以下错误:IOError: [Errno 2] No such file or directory: 'D' 我想我尝试未经许可读取文件,不知道如何修复此错误!顺便说一句,Ty 寻求帮助
标签: python path directory mime