I saw this code from a post here and wanted to see how it works: importing gmail text content to a text file with python returns nothing
But when I try it an error appears. PermissionError: [Errno 13] Permission denied: 'C:\\email1.txt'
this is the code:
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
I'm guessing it has something to do with access of the folder I created in the C: location however I don't know what to do.