多个FOR循环中的IF语句

时间:2020-09-28 03:17:34

标签: python

我对python还是很陌生,并且在if内嵌套for loop函数感到困惑

我只希望for loop根据if的返回值继续。

在这种情况下,我要遍历收件箱中的所有Message,如果它们的读取值为False,则遍历未读取的邮件附件。

但是,无论使用什么读取值,TrueFalse,我都会使用以下内容保存所有附件

for Message in mail_folders.get_messages():
    if Message.is_read == False:
        for att in Message.attachments:
            if '.html' in att.name:
                att.save(sv_path)

我完全写错了吗?还是我的缩进不正确?

更新

使用

for Message in mail_folders.get_messages():
    print(Message.is_read)
    if Message.is_read == False:
        for att in Message.attachments:
            if '.html' in att.name:
                att.save(sv_path)

print语句返回TrueFalse

谢谢

1 个答案:

答案 0 :(得分:0)

不能完全确定为什么,但是进行两项更改现在可以正常工作。

for m in mail_folders.get_messages(download_attachments = True):
    print(m.is_read)
    if m.is_read == False:
        for att in m.attachments:
            if '.html' in att.name:
                att.save(sv_path)