My simple script parse output screen and to a new file!

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

  1. chuck amadi

    chuck amadi Guest

    Hi again I've bee tryinh to get print statement output from the screen
    to a new file.
    Im using the for loop I assume my target can and is a reference variable
    and my object is mbox.
    I have tried a few variations the script runs ok but when I cat the the
    new file it's empty but I nedd the output to flush to this new file in
    order to utilise and populate a database at some point.
    Cheers

    Chuck

    #!/usr/bin/env python

    import sys
    import os
    import email
    import mailbox
    import StringIO

    # Open the testwws user mailbox (tmp user chuck)
    # fp denotes factory paraemeter
    # mode can be 'r' when the file will only be read, 'w' for only writing
    #(an existing file with the same name will be erased), and 'a' opens the
    file
    # for appending; any data written to the file is automatically added to
    the end.# 'r+' opens the file for both reading and writing. The mode.

    # The File SurveyResults.txt must all ready exist.

    #mailout = open("/home/chuck/pythonScript/SurveyResults.txt") # mode 'w'
    means open the file for writ-ing
    # (any data already in the file will be erased)
    #output.writelines(mailout)
    #mailout = file("/home/chuck/pythonScript/SurveyResults.txt")

    # open() returns a file object, and is most commonly used with two
    arguments:
    # "open(filename, mode)".
    fp = open("/home/chuck/pythonScript/testbox")

    # message_from_file returns a message object struct tree from an
    # open file object.

    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()


    # First open the testbox file to read(r) and write(w)to the
    SurveyResults.txt
    # open() has been depreicated use file()
    #fp = open("testbox","r")
    mailout = file("/home/chuck/pythonScript/SurveyResults.txt","a")

    # Read the testbox file into a list then copy to
    # new file.
    #for mail in fp.readlines():
    # for <target> in <object>
    for mail in mbox:
    mailout.write(mail)

    print "testbox file copied...to SurveyResults.txt"

    # Now close the files
    #fp.close()
    mailout.close()

    #mailout.close to close it and free up any system resources taken up by
    the open file.
    # After calling output.close(), attempts to use the file object will
    automatically fail.
    chuck amadi, Jun 10, 2004
    #1
    1. Advertising

  2. chuck amadi

    David Fisher Guest

    chuck amadi <> 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

    ><{{{*>
    David Fisher, Jun 10, 2004
    #2
    1. Advertising

  3. chuck amadi

    chuck amadi Guest

    David Fisher wrote:

    >chuck amadi <> 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
    >
    >
    >
    >><{{{*>
    >>
    >>

    Cheers I thought I was going mad with that for loop .

    Thanks
    chuck amadi, Jun 10, 2004
    #3
  4. chuck amadi

    Peter Otten Guest

    Chuck,

    if you want to take full advantage of Python's superior readability, reduce
    the number of comments. Instead of commenting out code, remove it and keep
    a copy of the old version around.
    You can write general notes about file modes or file() vs. open() on a
    post-it and stick it on your screen until you've memorized them. The really
    important on-topic stuff is best moved into doc-strings.
    You'll see your script magically shrink and both we and you will better
    understand what it does - trust me :)

    Peter
    Peter Otten, Jun 10, 2004
    #4
  5. chuck amadi

    Chuck Amadi Guest

    Hi from my script using the for loop Im able to print the email body messages
    to screen I have read the Handling files and Input and Output that discusses
    reading and writing to files . But I just need the generated data from the
    email body messages to parse into a new file not just print results to screen.

    Have got to create a Function as at the moment the write() Function just
    writes the string I need the output screen data from the print statement to
    file.

    Im also looking at the struct but I think Im tackling this wrong as I don't
    need to write to new file but parse to it.

    Any ideas.

    cheers chuck
    Chuck Amadi, Jun 10, 2004
    #5
  6. chuck amadi

    Chuck Amadi Guest

    Hi all got it to work using readlines() Thanks for your help folks.
    learnt quite alot hopefully read my notes and the three Python books as I
    bought some time to get to real use Python.

    Thanks again Python List.


    #!/usr/bin/env python

    import sys
    import os
    import email
    import mailbox
    import StringIO
    import struct

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

    # list of body messages.
    bodies = []

    # mail is the file object
    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,must
    # need to reinitalize it.

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

    mailout = file("/home/chuck/pythonScript/SurveyResults.txt","a")

    for line in fp.readlines():
    mailout.write(line)

    print "testbox file copied...to SurveyResults.txt cat to view"

    # Now close the files

    mailout.close
    Chuck Amadi, Jun 10, 2004
    #6
  7. chuck amadi

    Peter Otten Guest

    Chuck Amadi wrote:

    Code:
    
    Now it looks better :-)
    [color=blue]
    > # Now close the files
    >  
    > mailout.close[/color]
    
    Lest it bite you in the future: the above is basically a noop. The () in 
    
    mailout.close()
    
    is necessary to invoke the method.
    
    Peter
    Peter Otten, Jun 10, 2004
    #7
  8. chuck amadi

    Chuck Amadi Guest

    Cheers for pointing that out !.

    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)

    #<Instead of this >

    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.
    Chuck Amadi, Jun 10, 2004
    #8
  9. chuck amadi

    Chuck Amadi Guest

    Has anyone got a work around for readlines() So as the print statement parses
    only the bodies of the mail boxes don't need headers etc.

    Using the Email Module I have the following snippet thats works minus the
    readlines() and write() function reads and writes all data in the mailbox file.
    How do I use a for loop at the bottom to only write bodies of the mailbox as
    when I use the print statement.

    Cheers

    # mail is the file object
    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,must
    # need to reinitalize it.

    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)
    Chuck Amadi, Jun 10, 2004
    #9
  10. chuck amadi

    chuck amadi Guest

    Re: EMAIL script parse output screen and to a new file!

    Chuck Amadi wrote:

    >Cheers for pointing that out !.
    >
    >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)
    >
    >#<Instead of this >
    >
    >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 hav 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.
    >


    The above works but also parse's the email headers as well as the
    desired body messages.

    Cheers

    Chuck
    chuck amadi, Jun 13, 2004
    #10
  11. chuck amadi

    Chuck Amadi Guest

    Re: Script parse output screen but cant get desired output newfile!.

    On Thu, 2004-06-10 at 01:03, David Fisher wrote:
    > chuck amadi <> 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.
    Chuck Amadi, Jun 14, 2004
    #11
  12. Re: Script parse output screen but cant get desired output newfile!.

    In article <>,
    Chuck Amadi <> wrote:
    .
    .
    .
    >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.

    .
    .
    .
    There's a lot going on in your message. I *think* what you want
    is the suggestion to replace
    for mail in fp.readlines():
    mailout.write(mail)
    with
    mailout.write(fp.read())
    --

    Cameron Laird <>
    Business: http://www.Phaseit.net
    Cameron Laird, Jun 16, 2004
    #12
    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. John M. Lembo

    Parse Text File and Output to File

    John M. Lembo, Aug 1, 2003, in forum: Perl
    Replies:
    0
    Views:
    12,711
    John M. Lembo
    Aug 1, 2003
  2. chuck amadi

    simple script to read and parse mailbox

    chuck amadi, Jun 5, 2004, in forum: Python
    Replies:
    6
    Views:
    493
    Chuck Amadi
    Jun 7, 2004
  3. chuck amadi
    Replies:
    1
    Views:
    489
    Larry Bates
    Jun 23, 2004
  4. John M. Lembo

    Parse Text File and Output to File

    John M. Lembo, Aug 1, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    161
    JS Bangs
    Aug 1, 2003
  5. Mav
    Replies:
    22
    Views:
    365
Loading...

Share This Page