On Thu, Sep 29, 2011 at 11:28 PM, Dave Angel <d...@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
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to