[Tutor] Newbie in Python (fwd)

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Mar 16 18:01:05 CET 2005


[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




More information about the Tutor mailing list