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

C

chuck amadi

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.
 
D

David Fisher

chuck amadi said:
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
 
C

chuck amadi

David said:
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
 
P

Peter Otten

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
 
C

Chuck Amadi

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
 
C

Chuck Amadi

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
 
P

Peter Otten

Chuck Amadi wrote:

Code:
Now it looks better :-)
[QUOTE]
# Now close the files

mailout.close[/QUOTE]

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

mailout.close()

is necessary to invoke the method.

Peter
 
C

Chuck Amadi

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.
 
C

Chuck Amadi

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

chuck amadi

Chuck said:
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
 
C

Chuck Amadi

chuck amadi said:
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.
 
C

Cameron Laird

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top