get screen output to file using get_payload()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chuck amadi

    get screen output to file using get_payload()

    Hi I have managed to print the output of the get_payload to screen
    but I need to write to a file as I only require the email body messages
    from the mailbox.My script using the fp.readlines() function writes the
    entire contents of the mailbox of cause including the headers of the
    emails I do not want.

    I have tried a few things but I cant get to my goal.Any ideas or
    pointers I need only the email body and I cant figute out why I can
    using the print statement but get those results to a file.
    cheers

    import sys
    import os
    import email
    import mailbox
    import StringIO


    fp = file ("/var/spool/mail/chucka")


    mbox = mailbox.UnixMai lbox(fp, email.message_f rom_file)
    # list of body messages.
    bodies = []

    # mail is the file object
    for mail in mbox:
    print 'mail'
    print mail['Subject']
    print mail.get_conten t_type()#text/plain
    print mail.get_payloa d()


    mailout = file("/home/chucka/pythonScript/SurveyResults1. txt","r")


    fp = open("/var/spool/mail/chucka")
    mb = mailbox.UnixMai lbox(fp, email.message_f rom_file)

    #for bdymsg in fp.xreadlines() :
    #for bdymsg in fp.readlines():
    #for msgbodies in mb:
    # mailout.write(b dymsg)
    # bdymsg = mail.get_payloa d()
    # mailout.write(m ail.get_payload ()

    for bmsg in mb:
    bmsg = get_payload()
    mailout.write(b msg)
    # bmsg = [get_payload]
    print "mailbox file copied...to SurveyResults.t xt"

    # Now close the files
    mailout.close()




  • Lee Harr

    #2
    Re: get screen output to file using get_payload()

    On 2004-06-23, chuck amadi <chuck.amadi@nt lworld.com> wrote:[color=blue]
    > Hi I have managed to print the output of the get_payload to screen
    > but I need to write to a file as I only require the email body messages
    > from the mailbox.My script using the fp.readlines() function writes the
    > entire contents of the mailbox of cause including the headers of the
    > emails I do not want.
    >
    > I have tried a few things but I cant get to my goal.Any ideas or
    > pointers I need only the email body and I cant figute out why I can
    > using the print statement but get those results to a file.
    > cheers
    >[/color]
    [color=blue]
    > mailout = file("/home/chucka/pythonScript/SurveyResults1. txt","r")
    >[/color]


    You have the file opened read only. You would need something like:

    mailout = file("/home/chucka/pythonScript/SurveyResults1. txt", "w")

    Comment

    Working...