Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > My simple script parse output screen and to a new file!

Reply
Thread Tools

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

 
 
chuck amadi
Guest
Posts: n/a
 
      06-09-2004
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.


 
Reply With Quote
 
 
 
 
David Fisher
Guest
Posts: n/a
 
      06-10-2004
chuck amadi <(E-Mail Removed)> 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 ) usage is file(filename). Which makes sense
since it returns a file object not an 'open' object

><{{{*>

 
Reply With Quote
 
 
 
 
chuck amadi
Guest
Posts: n/a
 
      06-10-2004
David Fisher wrote:

>chuck amadi <(E-Mail Removed)> 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 ) 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


 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      06-10-2004
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


 
Reply With Quote
 
Chuck Amadi
Guest
Posts: n/a
 
      06-10-2004
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


 
Reply With Quote
 
Chuck Amadi
Guest
Posts: n/a
 
      06-10-2004
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



 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      06-10-2004
Chuck Amadi wrote:

[code]

Now it looks better

> # Now close the files
>
> mailout.close


Lest it bite you in the future: the above is basically a noop. The () in

mailout.close()

is necessary to invoke the method.

Peter

 
Reply With Quote
 
Chuck Amadi
Guest
Posts: n/a
 
      06-10-2004
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.




 
Reply With Quote
 
Chuck Amadi
Guest
Posts: n/a
 
      06-10-2004
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)



 
Reply With Quote
 
chuck amadi
Guest
Posts: n/a
 
      06-13-2004
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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Want help on how we convert output to tabular format Using the expat parser (http://expat.sourceforge.net/) i have to parse the following xml file and print it on the screen in tabular format. sharan XML 1 10-26-2007 01:20 PM
parse output screen ok but cant get desired output new file! chuck amadi Python 1 06-23-2004 02:16 PM
simple script to read and parse mailbox chuck amadi Python 6 06-07-2004 04:15 PM
simple script to read and parse mailbox Chuck Amadi Python 0 06-07-2004 03:00 PM
simple script to read and parse mailbox Chuck Amadi Python 0 06-07-2004 02:54 PM



Advertisments