[python] mailbox -- Question on usage

James Henderson james at logicalprogression.net
Mon Jun 14 17:53:43 CEST 2004

On Monday 14 June 2004 4:03 pm, David Stockwell wrote:
> Hi,
> After looking at http://docs.python.org/lib/module-mailbox.html  I am a bit
> confused.
> The bottom of the page refers to some shortcut usage for getting a mail box
> import email
> import mailbox
> mbox = mailbox.UnixMailbox(fp, email.message_from_file)
> My question is what is fp?   and how come you don't have to say
> email.message_from_file(fileName) on the second parameter?
> Is fp supposed to be something like this?
> myname = 'mymailboxfilename'
> fp = open(myname)
> Then
> mbox = mailbox.UnixMailbox(fp, email.message_from_file(myname) )  ????
> I tried executing the original statement in python (the one from the
> website) and it seemed to take it.
> So what does it mean to specify the second parameter like that?
> mbox = mailbox.UnixMailbox(fp, email.message_from_file)
> Finally, where can I find a list of all the apis that I can call for
> mailbox?
> So far I have only found one 'next()'  that returns a message object.
> Thanks in advance,
> David

>From the top of the page you were looking at:

class UnixMailbox(fp[, factory])	
Access to a classic Unix-style mailbox, where all messages are contained in a 
single file and separated by "From "(a.k.a. "From_") lines. The file object 
fp points to the mailbox file. The optional factory parameter is a callable 
that should create new message objects. factory is called with one argument, 
fp by the next() method of the mailbox object. The default is the 
rfc822.Message class (see the rfc822 module - and the note below).

So "fp" is a file object as you rightly surmise, and "email.message_from_file" 
is acting as the factory function.  As it says, the factory can be any 
function that takes a file object and returns a message object.

The public API consists entirely of the next() method and the contructors 
listed on the page you're looking at.  Being iterables you should also be 
able to use mailbox objects in the many of the same ways that you use other 

James Henderson, Logical Progression Ltd.

More information about the Python-list mailing list