[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 Sep 29 17:39:44 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)
>
>>> print splitext.__doc__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'splitext' is not defined
>>> print slices.__doc__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'slices' is not defined
>>> print slices._doc_
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'slices' is not defined
Thanks,
>
> ==
> DaveA
>
>
>
--
Best Regards,
lina
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110929/3ef4d0f3/attachment-0001.html>
More information about the Tutor
mailing list