在此代码中的何处以及如何在其中插入“ try andexcept”,以避免程序崩溃?

时间:2018-10-27 00:49:44

标签: python

我有这段代码可以下载电子邮件并保存文本正文。使用主题字段中的文本作为文件名,将文本正文保存为文本文件。主题应该基本上是学生编号+学校星期编号,例如:
1234567891week8

pathToFiles = '/home/pedro/getEmailtexts/emailTexts17BE/'
server = IMAPClient(HOST, use_uid=True, ssl=True)
server.login(USERNAME, PASSWORD)
select_info = server.select_folder('Inbox')
unseenMessages = server.search(['UNSEEN'])
print('Number of unseen messages is ' + str(len(unseenMessages)))

for uid, message_data in server.fetch(unseenMessages, 'RFC822').items():
        email_message = email.message_from_bytes(message_data[b'RFC822'])
        print(' message UID is ' + str(uid))
        print(email_message.get('Subject'))
        messageSubject = email_message.get('Subject')
        file = messageSubject + '.txt'
        theFile = open(pathToFiles + file, 'w')
        rawMessage = server.fetch(unseenMessages, ['BODY[]', 'FLAGS'])
        message = pyzmail.PyzMessage.factory(rawMessage[uid][b'BODY[]'])
        text =  message.text_part.get_payload().decode(message.text_part.charset)
        saveText = text.rstrip()
        theFile.write(saveText)
        theFile.close()

但是,有些学生或其电子邮件程序在主题字段中添加了一些奇怪的内容,从而导致故障。这是我的bash终端的示例输出:

1725010108week8
message UID is 33
1725010135week8
message UID is 34
1725010126 week8
message UID is 35
������������������1725010118week8
Traceback (most recent call last):
File "./getAnswersFromEmail17BEv2.py", line 45, in <module>
file = messageSubject + '.txt'
TypeError: unsupported operand type(s) for +: 'Header' and 'str'
pedro@pedro-newssd:~/getEmailtexts/python$ 

我必须转到电子邮件,删除有问题的电子邮件,然后重新开始。

我认为我可以在其中插入尝试...除了...,但是我看不出具体如何。也许还有其他方法可以解决一个狡猾的话题

您对业余爱好者有什么窍门,如何解决?对于此类错误,如何使程序转到下一封电子邮件?

2 个答案:

答案 0 :(得分:2)

我的第一个建议是将messageSubject变量转换为字符串。

file = str(messageSubject) + '.txt'

要使用tryexcept子句,以下代码段如果无法为电子邮件创建file变量,则允许该代码移至下一封电子邮件

try:
    file = messageSubject + '.txt'
except TypeError:
    continue

答案 1 :(得分:0)

@ Alexis Lucattini:非常感谢!

这解决了疯狂的主题问题:

messageSubject = str(email_message.get('Subject'))

然后我得到了“ AttributeError:'NoneType'对象没有属性'get_payload'”

最后,我同时使用了两个建议。

至少1名学生必须发送一封空电子邮件,这会导致如下问题:

UID is 421
1825010336week8
Message subject is 1825010336week8
UID is 424
1825010334Week
Message subject is 1825010334Week
UID is 425
=?gb18030?B?MTgyNTAxMDIzNyDA7s7Ex78=?=
Message subject is =?gb18030?B?MTgyNTAxMDIzNyDA7s7Ex78=?=
UID is 426
=?gb18030?B?ufnT7ubDIDE4MjUwMTAyNDQ=?=
Message subject is =?gb18030?B?ufnT7ubDIDE4MjUwMTAyNDQ=?=
UID is 430
=?gb18030?B?MTgyNTAxMDExMyxBLEIsQSxCLEEsQyxELEcsQSxD?=
 =?gb18030?B?LEYsSCxBLEQsQixBLEgsRyxDLEEsQSxCLEEsQSxC?=
 =?gb18030?B?LEIsRixELEosRSxBLEcsQyxILEmBMIQyCgoK?=
Message subject is =?gb18030?B?MTgyNTAxMDExMyxBLEIsQSxCLEEsQyxELEcsQSxD?=
 =?gb18030?B?LEYsSCxBLEQsQixBLEgsRyxDLEEsQSxCLEEsQSxC?=
 =?gb18030?B?LEIsRixELEosRSxBLEcsQyxILEmBMIQyCgoK?=
UID is 431
=?gb18030?B?MTgyNTAxMDEzMSxBLEIsQSxCLEEsQyxELEcsQSxD?=
 =?gb18030?B?LEYsSCxBLEQsQixBLEgsRyxDLEEsQSxCLEEsQSxC?=
 =?gb18030?B?LEIsRixELEosRSxBLEksQyxILEcKCgo=?=
Message subject is =?gb18030?B?MTgyNTAxMDEzMSxBLEIsQSxCLEEsQyxELEcsQSxD?=
 =?gb18030?B?LEYsSCxBLEQsQixBLEgsRyxDLEEsQSxCLEEsQSxC?=
 =?gb18030?B?LEIsRixELEosRSxBLEksQyxILEcKCgo=?=
UID is 432
1825010207week8
Message subject is 1825010207week8
UID is 434
������������������1825010136week7
Message subject is ������������������1825010136week7
Traceback (most recent call last):
  File "./getAnswersFromEmail18BEv2.py", line 52, in <module>
    text = message.text_part.get_payload().decode(message.text_part.charset)
AttributeError: 'NoneType' object has no attribute 'get_payload'
pedro@pedro-newssd:~/getEmailtexts/python$ ^C
pedro@pedro-newssd:~/getEmailtexts/python$ 

因此,我尝试了该错误以外的尝试:

for uid, message_data in server.fetch(unseenMessages, 'RFC822').items():
        email_message = email.message_from_bytes(message_data[b'RFC822'])
        print('UID is ' + str(uid))
        print(email_message.get('Subject'))
        messageSubject = str(email_message.get('Subject'))
        print('Message subject is ' + messageSubject)
        if messageSubject == None:
            messageSubject = 'idiot'
        file = messageSubject + '.txt'
        theFile = open(pathToFiles + file, 'w')
        rawMessage = server.fetch(unseenMessages, ['BODY[]', 'FLAGS'])
        try:
            message = pyzmail.PyzMessage.factory(rawMessage[uid][b'BODY[]'])
            text = message.text_part.get_payload().decode(message.text_part.charset)
            saveText = text.rstrip()
            theFile.write(saveText)
            theFile.close()
        except AttributeError:
            continue

之后,所有电子邮件下载都没有问题。 很好,因为现在我可以使用此例程和标记答案并将分数写入Excel文件的例程,并在1个程序中一起使用。

谢谢您的建议!