编程语言
首页 > 编程语言> > 使用python从mbox文件夹中读取邮件

使用python从mbox文件夹中读取邮件

作者:互联网

我想从mbox文件中读取邮件并根据它执行一些操作.我不想编写/修改mbox文件,但它会被另一个进程修改(主要是添加新邮件).

我正在阅读这份文件. http://docs.python.org/library/mailbox.html#mailbox.mbox

但我不明白以下几点

>我应该在阅读邮件之前调用lock()吗? (我不是写信给
文件)
>无论如何,我可以在其他进程时收到回调
(thunderbird)修改文件?
>文件中的更改是否反映在mbox对象中?我的意思是,在创建mbox对象后,如果将新邮件添加到文件中,我是否会使用该对象访问该邮件?或者我应该再次创建一个新对象?

PS:我不允许安装雷鸟的任何插件:(

解决方法:

>当您不修改mbox时,无需调用Mailbox.lock().来自Mailbox.lock()文档的引用(强调我自己):

You should always lock the mailbox before making any modifications to
its contents.

>文件更改通知超出了邮箱模块的范围.在Linux系统上,您可以使用pyinotify来获得此功能.
> documentation也回答了这个问题:

The default Mailbox iterator iterates over message representations, not keys as the
default dictionary iterator does. Moreover, modification of a mailbox during iteration
is safe and well-defined. Messages added to the mailbox after an iterator is created
will not be seen by the iterator. Messages removed from the mailbox before the
iterator yields them will be silently skipped, though using a key from an iterator
may result in > a KeyError exception if the corresponding message is subsequently
removed.

简而言之,您需要在mbox文件更改后创建新的邮箱实例.

标签:python,mbox
来源: https://codeday.me/bug/20190704/1377699.html