[Tutor] a quick Q: how to use for loop to read a series of files with .doc end

lina lina.lastname at gmail.com
Fri Oct 7 18:35:07 CEST 2011


#!/usr/bin/python3

import os.path
import glob

TOKENS="BE"
LINESTOSKIP=0
INFILEEXT=".xpm"
OUTFILEEXT=".txt"


if __name__=="__main__":

    for fileName in glob.glob('*.xpm'):
        base, ext =os.path.splitext(fileName)
        text=open(fileName).readlines()
        text=text[LINESTOSKIP:]
        numcolumns=len(text[0])
        results={}
        for ch in TOKENS:
            results[ch] = [0]*numcolumns
        for line in text:
            line = line.strip()
            for col, ch in enumerate(line):
                if ch in TOKENS:
                    results[ch][col]+=1
        summary=[]
        for a,b in zip(results['E'],results['B']):
            summary.append(a+b)
        print(summary)
        open(base+OUTFILEEXT,"w").write(str(summary))

I test it, it finally works. (I was not so sensitive about indentation also)

If you have time or interest, hope to help polish the code,
and warmly welcome to make above one a bit sophisticated. better not
easy-readable for me at first sight.

Thanks again,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111008/c7b413a7/attachment.html>


More information about the Tutor mailing list