Reading mail from mbox folder using python

I want to read letters from the mbox file and perform some actions based on this. I do not want to write / modify the mbox file, but it will be modified by another process (most often adding new letters).

I am reading this documentation. http://docs.python.org/library/mailbox.html#mailbox.mbox

But I do not understand the following

  • Should I call lock () before reading mail? (I am not writing a file)
  • Anyway, can I get a callback when another process (thunderbird) modifies the file?
  • Are file changes reflected in the mbox object? I mean, after creating the mbox object, if a new letter is added to the file, will I get access to the message using the object? Or should I create a new object again?

PS: I'm not allowed to install any thunderbird plugins :(

+4
source share
1 answer
  • No need to call Mailbox.lock() when you are not Mailbox.lock() mbox. Quote from the documentation of Mailbox.lock() (highlighting my own):

    You should always block your mailbox before making any changes . its contents.

  • Notification of changes to the file is beyond the scope of the mailbox module. On Linux systems, you can use pyinotify to get this functionality.

  • This is also stated in the documentation :

    The default mailbox iterator iterates over messages, not keys, like the default dictionary iterator. In addition, changing the mailbox during iteration is safe and well defined. Messages added to the mailbox after creating the iterator will not be visible to the iterator. Messages deleted from the mailbox before the iterator issues them will be silently skipped, although using the key from the iterator can lead to a> KeyError exception if the corresponding message is subsequently deleted.

    In short, you need to create a new instance of Mailbox after modifying the mbox file.

+2
source

Source: https://habr.com/ru/post/1392060/


All Articles