0

我正在使用此脚本从收件箱中删除邮件。

if(not debug):
  logging.debug("removing messages")
  all_mail.lock()
  for message in all_mail:
    all_mail.remove(message)
  all_mail.flush()
  all_mail.unlock()
all_mail.close()

运行此脚本一次后,我注意到/var/spool/mail. 如果我再次尝试运行脚本,我会得到一个相当可预测的异常:mailbox.ExternalClashError: dot lock unavailable

所以看起来 all_mail.unlock() 不起作用,但我不确定还能做什么。

4

1 回答 1

0

您的脚本应该引发异常,all_mail.remove(message)因为它永远不会到达unlock调用。与普通字典的mbox重要区别,这是您的问题:

默认邮箱迭代器迭代消息表示,而不是像默认字典迭代器那样迭代键。

这意味着for message in all_mail:makemsg包含 amboxMessage而不是键并且remove引发KeyError异常。

修复很简单:

for message in all_mail.iterkeys():
    all_mail.remove(message)
于 2017-08-24T08:11:26.077 回答