import mailbox
import email
import json
MBOX = 'resources/ch06-mailboxes/data/orthpole.mbox'
def objectify_message(msg):
# Map in fields from the message
o_msg = dict([ (k, v) for (k,v) in msg.items() ])
# Assume one part to the message and get its content
# and its content type
part = [p for p in msg.walk()][0]
o_msg['contentType'] = part.get_content_type()
o_msg['content'] = part.get_payload()
return o_msg
mbox = mailbox.UnixMailbox(open(MBOX, 'rb'), email.message_from_file)
messages = []
while 1:
msg = mbox.next()
if msg is None: break
messages.append(objectify_message(msg))
print json.dumps(messages, indent=1)
IOError Traceback(最近一次调用最后一次) in() #消息到方便的JSON表示
mbox = mailbox.UnixMailbox(open(MBOX,'rb'),email.message_from_file)
messages = []
IOError:[Errno 2]没有这样的文件或目录:'resources / ch06-mailboxes / data / orthpole.mbox'