simple script to read and output Mailbox body to file.

Chuck Amadi chuck at smtl.co.uk
Wed Jun 9 10:19:36 EDT 2004


Hi please could someone point to what I have done wrong as the script runs 
with no errors but the file I am trying to parse the print statement output 
too instead of just the screen/command line to another file.

I have digested the Input and Output and text formatting docs and I read 
something about using stdout to a file . Can someone point me in the right 
direction.

Cheers my code below.

###############################################################
## This script will open and parse email messages body content.
## This Python script will reside on Mail Server on ds9:
## Emails are all plain/text you could just write the following
## Which will leave a list of strings , each one a message body.
## The Survey User is testwws and the .procmailrc file folder is
## Survey . i.e /home/testwws/Mail/inbox/Survey .
###############################################################
## file:getSurveyMail.py Created : 06/06/04 Amended date: 09/06/04
## Revision 1. make copy to continue called getSurveyMailRev2.py
###############################################################
 
#The following line makes it run itself(executable script on UN*X)
#!/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
#fp = open("testbox","r")
mailout = open("/home/chuck/pythonScript/SurveyResults.txt","w")
 
# Read the testbox file into a list then copy to
# new file.
for mail in fp.readlines():
    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.
 
 






More information about the Python-list mailing list