Python:将电子邮件导入文本文件导致PermissionError

时间:2017-07-12 00:00:17

标签: python python-3.x imaplib

我在这里的帖子中看到了这段代码,想看看它的工作原理:importing gmail text content to a text file with python returns nothing

但是当我尝试它时会出现错误。 PermissionError: [Errno 13] Permission denied: 'C:\\email1.txt'

这是代码:

import imaplib
import email
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('myemail', 'mypassword')
mail.list()
mail.select('inbox')

result, data = mail.uid('search', None, "ALL")
i = len(data[0].split())
for x in range(i):
    latest_email_uid = data[0].split()[x]
    result, email_data = mail.uid('fetch', latest_email_uid, '(RFC822)')
    raw_email = email_data[0][1]

    raw_email_string = raw_email.decode('utf-8')
    email_message = email.message_from_string(raw_email_string)
for part in email_message.walk():
    if part.get_content_type() == "text/plain":
        body = part.get_payload(decode=True)
        save_string = "C:\\email" + str(x) + ".txt"
        myfile = open(save_string, 'a')
        myfile.write(str(body))
        myfile.close()
    else:
        continue

我猜测它与访问我在C:位置创建的文件夹有关,但我不知道该怎么做。

0 个答案:

没有答案