[Tutor] using 'mailbox'

Rick Pasotto rick@niof.net
Fri, 7 Sep 2001 15:16:54 -0400


On Fri, Sep 07, 2001 at 11:24:17AM -0700, Sheila King wrote:
> On Fri, 7 Sep 2001 14:09:19 -0400, Rick Pasotto <rick@niof.net>  wrote
> about [Tutor] using 'mailbox':
> 
> :What am I overlooking? I have a file in unix mailbox format that I would
> :like to process by changing the body of each message. Using the mailbox
> :module I can get and deal with the headers but I'm not seeing how to
> :deal with the body.
> 
> from the mailbox module docs:
> "By defaul this is an rfc822.Message object (see the rfc822 module)."
> 
> The rfc822.Message object is only the headers. You need to read in the
> message body separately.

This is what I didn't know how to do. Just figured it out.

>>> import mailbox
>>> filep = open('mbox')
>>> mb = mailbox.UnixMailbox(filep)
>>> msg = mb.next()
>>> body = msg.fp.readlines()

Now msg contains all the headers and body contains the rest of the
message.

Repeat the last two lines as needed.

-- 
Certain nations seem particularly liable to fall prey to
governmental plunder. They are those in which men, lacking faith
in their own dignity and capability, would feel themselves lost if
they were not governed and administered every step of the way.
	-- Frédéric Bastiat (1801-1850)
    Rick Pasotto    rickp@telocity.com    http://www.niof.net