Script parse output screen but cant get desired output new file!.

Chuck Amadi chuck at smtl.co.uk
Mon Jun 14 12:06:48 EDT 2004


On Thu, 2004-06-10 at 01:03, David Fisher wrote:
> chuck amadi <chuck.amadi at ntlworld.com> writes:
> > fp = open("/home/chuck/pythonScript/testbox")
> >  mbox = mailbox.UnixMailbox(fp, email.message_from_file)
> > for mail in mbox:
> >         print mail['Subject']
> >         print mail.get_content_type()#text/plain
> >         print mail.get_payload()
> If your going to use the UnixMailbox in two different loops, you'll
> need to reinitalize it.  Just call:
> 
> fp = open("/home/chuck/pythonScript/testbox")
> mbox = mailbox.UnixMailbox(fp, email.message_from_file)
> 
> again before you use 'mbox' again.  Otherwise the file pointer is
> still pointing at the end of the file which is why you get nothing the
> second time.  Or alternately, just to be more confusing :), use:
> 
> for mail in mailbox.UnixMailbox(open("/home/chuck/pythonScript/testbox"), \
>                                 email.message_from_file):
>         print mail['Subject']
>         print mail.get_content_type()#text/plain
>         print mail.get_payload()
> 
> Which does it all in one stroke.
> 
> Oh, minor nitpick.  open(filename) is depredicated I believe.  The new
> prefered (for now :P) usage is file(filename).  Which makes sense
> since it returns a file object not an 'open' object
> 
> ><{{{*>

By the way list is there a better way than using the readlines() to
parse the mail data into a file , because Im using
email.message_from_file it returns 
all the data i.e reads one entire line from the file , headers as well
as just the desired body messages .

fp = file("/home/chuck/pythonScript/testbox")
mb = mailbox.UnixMailbox(fp,
email.message_from_file)                          


mailout = file("/home/chuck/pythonScript/SurveyResults.txt","w")
for mail in fp.readlines():
    mailout.write(mail)

Something like this>

for mail in mb:
        body = mail.get_payload()
	mailout.write(body) # write only the body messages to SurveyResults.txt

Cheers if the is a better way I can't get my head round how I can print
mail ( 
only the body messages) to screen and the entire mail headers and body
to the new file.

Hi have any one got any suggstions to my script I can parse the email
body messages to screen but I want the same desired effect to save to a
new file.I have tried a few things to no effect.







More information about the Python-list mailing list