[Tutor] Newbie in Python (fwd)

Liam Clarke cyresse at gmail.com
Wed Mar 16 22:50:41 CET 2005


Hi Oscar, 

processing emails... you want to use the email module. 

I believe, you'd go like this (and this is rough, and untested, docs are 
http://docs.python.org/lib/node565.html)

>>>import email
>>>parser = email.HeaderParser() 
>>> msgFile = file('email.txt','r')
>>> msgObj = parser.parse(msgFile)
>>> print msgObj['subject']

Try that out, it should do it for you... might have to tweak it, as I
don't the intepreter here.

Regards, 

Liam Clarke


On Wed, 16 Mar 2005 09:01:05 -0800 (PST), Danny Yoo
<dyoo at hkn.eecs.berkeley.edu> wrote:
> 
> [Forwarding to tutor at python.org.  Oscar, when you reply next time, please
> use your email client's Reply-to-all feature.  Otherwise, no one else will
> see the message.]
> 
> 
> ---------- Forwarded message ----------
> Date: Wed, 16 Mar 2005 23:03:13 +1100
> From: oscar ng <oscar2 at aapt.net.au>
> To: 'Danny Yoo' <dyoo at hkn.eecs.berkeley.edu>
> Subject: RE: [Tutor] Newbie in Python (fwd)
> 
> Hi Danny,
> 
> Thanks for the reference.  I managed to read all the folders and am able
> to open all the files in the directory using a for loop
> Here is the code:
> 
> def getFiles(parent):
>     #used to get the parent directory for the files to process
> 
>       mainFolder = os.listdir(parent)
> 
>       for a in mainFolder:
>           if os.path.isdir(parent + "/" + a):
>               subfolders = [a]          ##set the subfolders to the its
> ##categories
>               print subfolders
>               getFiles(parent + "/" + a)
> 
>           else:
> 
>              fileName = [parent + "/" + a]
> 
>              listFile = a       #listFile is a list that holds the name
> of                                        #files
> 
>              #print listFile,
> 
>              #listFile[a] +=1
>              #print listFile
> 
>              words=[]
>              for b in fileName:
>                 inputFile = open(b)         #inputFile holds the
> #individual/basic files to be
> #processed
>                 listText = inputFile.read() #listText holds entire texts
> of                                                        #all the files
> from parent
> #directory
> 
>                 print listText
> 
> I need now to ask how I can process the headers "subject" of each
> individual email/file as the content of the "subject" determines the
> appropriate category I need to place them into.  As listtext only prints
> all the emails in the folder.
> 
> Also is there a way I can count the number of files ie  listFile[a] +=1
> as this keeps giving errors when running it.
> 
> Thanks heaps
> 
> Oscar
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list