[Tutor] a quick Q: how to use for loop to read a series of files with .doc end
Dave Angel
d at davea.name
Thu Sep 29 17:28:29 CEST 2011
(Please don't top-post. Put your remarks AFTER the part you're quoting
from the previous message)
On 09/29/2011 10:55 AM, lina wrote:
> import os.path
>
> tokens=['E']
> result=[]
>
> for fileName in os.listdir("."):
> if os.path.isfile(fileName) and os.path.splitext(fileName)=="xpm":
> filedata = open(fileName)
> text=filedata.readlines()
> for line in text:
>
>
> How can I read from line 24 and do further looking for "E".
>
> Thanks,
>
>
As I said in my earlier message, this was untested. It gave you the
building blocks, but was not correct.
In particular, that if-test will always fail, so you're not seeing any
files.
import os.path
tokens=['E']
result=[]
for fileName in os.listdir("."):
if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==".xpm":
filedata = open(fileName)
text=filedata.readlines()
for line in text:
print line
Once you've tested that, then you're ready to just look at line 24.
text is a list, so you can refer to line 24 as text[24]
Or you can get lines 24-28, with text[24, 29] (look up slices in the
Python doc)
==
DaveA
More information about the Tutor
mailing list