msg.walk()

S

Samuel Wright

Hi Guys

Using Python 2.3 here, trying to parse a MBOX email file using the code below:

------------------------

mailboxfile = 'emails.txt'

import email
import email.Errors, email.Parser, email.Message
import mailbox

def msgfactory(fp):
try:
return email.message_from_file(fp)
except email.Errors.MessageParseError:
# Don't return None since that will
# stop the mailbox iterator
return ''


def main():
fp = open(mailboxfile, 'r')
mbox = mailbox.UnixMailbox(fp, msgfactory)
for msg in mbox:
print msg
for part in msg.walk():
print part

if __name__=="__main__":
main()
 
P

pythonhda

Hi Guys

Using Python 2.3 here, trying to parse a MBOX email file using the
code below:

[...]
def msgfactory(fp):
try:
return email.message_from_file(fp)
except email.Errors.MessageParseError:
# Don't return None since that will
# stop the mailbox iterator
return ''

Notice the return ''
def main():
fp = open(mailboxfile, 'r')
mbox = mailbox.UnixMailbox(fp, msgfactory)
for msg in mbox:
print msg
for part in msg.walk():
print part
[...]

You have to do a test for an empty string in your main method (like the docs say).
 
S

Samuel Wright

"Notice the return ''"
def main():
fp = open(mailboxfile, 'r')
mbox = mailbox.UnixMailbox(fp, msgfactory)
for msg in mbox:
print msg
for part in msg.walk():
print part
[...]

"You have to do a test for an empty string in your main method (like
the docs say)."

Gotcha. I had assumed that was for mailboxes that might not be well
formatted, and I was checking a mailbox I had exported myself...

Thanks loads.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top