get screen output to file using get_payload()

Discussion in 'Python' started by chuck amadi, Jun 23, 2004.

  1. chuck amadi

    chuck amadi Guest

    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.UnixMailbox(fp, email.message_from_file)
    # list of body messages.
    bodies = []

    # mail is the file object
    for mail in mbox:
    print 'mail'
    print mail['Subject']
    print mail.get_content_type()#text/plain
    print mail.get_payload()


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


    fp = open("/var/spool/mail/chucka")
    mb = mailbox.UnixMailbox(fp, email.message_from_file)

    #for bdymsg in fp.xreadlines():
    #for bdymsg in fp.readlines():
    #for msgbodies in mb:
    # mailout.write(bdymsg)
    # bdymsg = mail.get_payload()
    # mailout.write(mail.get_payload()

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

    # Now close the files
    mailout.close()
    chuck amadi, Jun 23, 2004
    #1
    1. Advertising

  2. chuck amadi

    Lee Harr Guest

    On 2004-06-23, chuck amadi <> wrote:
    > 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
    >


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



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

    mailout = file("/home/chucka/pythonScript/SurveyResults1.txt", "w")
    Lee Harr, Jun 23, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chuck amadi
    Replies:
    1
    Views:
    360
    Piet van Oostrum
    Jun 23, 2004
  2. chuck amadi
    Replies:
    0
    Views:
    334
    chuck amadi
    Jun 22, 2004
  3. Chuck Amadi
    Replies:
    0
    Views:
    510
    Chuck Amadi
    Jun 22, 2004
  4. chuck amadi
    Replies:
    1
    Views:
    477
    Larry Bates
    Jun 23, 2004
  5. Replies:
    2
    Views:
    1,201
    Larry Bates
    Oct 14, 2005
Loading...

Share This Page