The following code downloads the attachments from the outlook email having a particular subject line (ADP Files). When i execute the script it generates an error saying FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\qwerty\\desktop\\EMAIL ADP\\attachments\\1494075600130_1640807852.xls'
.
What could be the problem?
import imaplib
import email
import os
svdir = 'C:\\Users\\rnandipati\\desktop\\EMAIL ADP'
mail = imaplib.IMAP4_SSL('outlook.office365.com',993)
mail.login("[email protected]", "R7!")
mail.select("Inbox")
typ, msgs = mail.search(None, '(SUBJECT "ADP Files")')
msgs = msgs[0].split()
for emailid in msgs:
resp, data = mail.fetch(emailid, "(RFC822)")
email_body = data[0][1]
m = email.message_from_bytes(email_body)
if m.get_content_maintype() != 'multipart':
continue
for part in m.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
filename = part.get_filename()
if filename is not None:
sv_path = os.path.join(svdir, filename)
if not os.path.isfile(sv_path):
print(sv_path)
fp = open(sv_path, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
Don't use the IF NOT
clause and it should work perfectly :)