Program not Stripping Headers from Email though Working fine

D

dont bother

Hey,
I have written this simple code. I want to strip all
the headers from a file including To, From, Received,
X-Spam etc which has long list of email messages (spam
archive).

After I do this: I want to write the payload in
another file output.
The code is here, but I dont know why the headers are
still present in the output file.

Please do help,
Whats missing friends?
Any help is highly appreciated

Thanks
Dont

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


import email
import os
import sys
import re
import mailbox
import email.Parser
import email.Message
import getopt


fread = open('325.r2', 'r')
msg=email.message_from_file(fread)

for hdr in msg.keys():
if hdr.startswith('From'):
del msg[hdr]
if hdr.startswith('To'):
del msg[hdr]
if hdr.startswith('Received'):
del msg[hdr]
if hdr.startswith('X-'):
del msg[hdr]

fwrite = open('output','w')
fwrite.write(msg.as_string())


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











__________________________________
Do you Yahoo!?
Yahoo! Search - Find what you’re looking for faster
http://search.yahoo.com
 
B

Ben Finney

for hdr in msg.keys():
if hdr.startswith('From'):
del msg[hdr]

Don't modify the list you're iterating over. Make a copy, and iterate
over that.

msg_keys = msg.keys()
for hdr in msg_keys:
if hdr.startswith( 'From' ):
del msg[hdr]
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top