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

lina lina.lastname at gmail.com
Thu Oct 6 18:21:16 CEST 2011


<snip>

>
>
> As for splitting into functions, consider:
>
> #these two are capitalized because they're intended to be constant
> TOKENS = "BE"
> LINESTOSKIP = 43
> INFILEEXT = ".xpm"
> OUTFILEEXT = ".txt"
>
> def dofiles(topdirectory):
>    for filename in os.listdr(topdirectory):
>        processfile(filename)
>
> def processfile(infilename):
>    base, ext =os.path.splitext(fileName)
>    if ext == INFILEEXT:
>        text = fetchonefiledata(infilename)
>        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
>
I still have trouble understanding the results[ch][col] part.

Thanks ahead,


>        writeonefiledata(base+**OUTFILEEXT, results)
>
> def fetchonefiledata(inname):
>    infile = open(inname)
>    text = infile.readlines()
>    return text[LINESTOSKIP:]
>
> def writeonefiledata(outname):
>    outfile = open(outname, "w")
>    ...process the results as appropriate...
>    ....(since you didn't tell us how multiple tokens were to be displayed)
>
> if __name__ == "__main__":
>    dofiles(".")     #or get the top directory from the sys.argv variable,
> which is set from command line.
>
>
>
>  --
>
> DaveA
>
>


-- 
Best Regards,

lina
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111007/d060262a/attachment.html>


More information about the Tutor mailing list