我尝试从gmail帐户下载附件文件时遇到此错误。
fp = open(filename, 'wb')
TypeError: expected str, bytes or os.PathLike object, not NoneType
以前,我可以从此脚本进行流畅的下载,但是现在出现此错误。请帮我解决脚本的问题吗?
import imaplib, email
import io
#log in and select the inbox
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('youmailid@gmail.com', 'yourpassword')
mail.select('testing')
#get uids of all messages
result, data = mail.uid('search', None, 'ALL')
uids = data[0].split()
print (uids)
#read the lastest message
result, data = mail.uid('fetch', uids[-1], '(RFC822)')
m = email.message_from_string(data[0][1].decode())
if m.get_content_maintype() == 'multipart':
for part in m.walk():
if part.get_content_maintype() == 'multipart': continue
if part.get('Content-Disposition') is None: continue
filename = part.get_filename()
fp = open(filename, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
print (f'{filename} saved!')
答案 0 :(得分:0)
看起来filename
是None
添加条件
if filename is None: continue
行后
filename = part.get_filename()