[Tutor] a quick Q: how to use for loop to read a series of files with .doc end
lina
lina.lastname at gmail.com
Tue Oct 4 18:38:52 CEST 2011
For file:
aaEbb
aEEbb
EaEbb
EaEbE
the expected output is
2 1 0 1
In the first column there is 2 E, so the output is 2 E, second column is 1
E.
#!/bin/python
import os.path
tokens=['B','E']
for fileName in os.listdir("."):
result=[]
if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==".xpm":
filedata = open(fileName)
text=filedata.readlines()
for line in text[0:]:
result.append({t:line.strip().count(t) for t in tokens})
for index,r in enumerate(result):
outfiledata=open("fileName.txt","w")
Thanks,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111005/b2e6560a/attachment.html>
More information about the Tutor
mailing list