[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 16:26:46 CEST 2011
On Thu, Sep 29, 2011 at 11:28 PM, Dave Angel <d at davea.name> wrote:
> (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
>
>
> Thanks for former help,
but I wonder how to output (write) the final result in each respectively
fileName with just different extension, such as original a.xpm write to
a.txt
Thanks,
#!/bin/python
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[23:len(text)-1]:
result.append({t:line.count(t) for t in tokens})
for index,r in enumerate(result):
fileName.txt.write(index,"-----",r)
???
--
Best Regards,
lina
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111004/b8332449/attachment-0001.html>
More information about the Tutor
mailing list