[Help]: mailbox classes

Alex Martelli aleax at aleax.it
Thu Sep 19 03:42:37 EDT 2002


J. Li wrote:

> Hi,
> 
> I am trying to extract a message from a unix mailbox file. Python does
> provide few classes for easy and uniform access. So it says! After
> having read the libiary references, I went nowhere.
> 
> Can anyone give me an example as how to use these mailbox classes and

Say I want to know how many messages from GvR are in my inbox:

[alex at lancelot alex]$ python
Python 2.2.1 (#2, Jul 15 2002, 17:32:26)
[GCC 2.96 20000731 (Mandrake Linux 8.1 2.96-0.62mdk)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from mailbox import UnixMailbox as MB
>>> inbox = MB(open('/home/alex/Mail/inbox'))
>>> from_guido = [x for x in inbox if x.get('from').find('Guido van 
Rossum')>-1]>>> len(from_guido)
108
>>>

Further, let's remove those such messages that are in the
python-dev mailing list:

>>> gvr_notdev = [x for x in from_guido if 
x.get('sender').find('python-dev')==-1]
>>> len(gvr_notdev)
5
>>>

etc, etc.


> what is it relationship with email.message classes.

Not much, intrinsically -- the items in a mailbox object by
default are rfc822.Message instances.  But you can pass as
the second argument to the constructor a factory with a
compatible interface, which should let you get email.Message
instances instead, if that's what you want.


Alex




More information about the Python-list mailing list