[Tutor] List and Indexing error.
SA
sarmstrong13@mac.com
Wed, 14 Aug 2002 07:05:48 -0500
On 8/14/02 1:26 AM, "Gregor Lingl" <glingl@aon.at> wrote:
> SA schrieb:
>
>> I'm getting a persistent indexing error whenever I run the following script:
>>
>> #!/usr/bin/env python
>>
>> import re
>> import os
>> import string
>>
>> list = os.listdir('.')
>> l = len(list)
>> print "The number of items in this dir is: ", l
>> for i in range(l):
>> input = open(list[i], "rb")
>> text = input.read()
>> input.close()
>> text2 = re.findall(r".*", text)
>> if text2[78] == "<hr>":
>> text3 = string.join(text2[79:])
>> output = open(list[i], "wb")
>> output.write(text3)
>> output.close()
>> else:
>> print list[i] + " does not have the correct begin length ..."
>>
>> Basically, I have a folder full of html files. I need to get rid of the
>> first part of each file. I figured the correct index. But when I run it I
>> get the following:
>> The number of items in this dir is: 148
>> Traceback (most recent call last):
>> File "./repy2.py", line 20, in ?
>> if text2[78] == "<hr>":
>> IndexError: list index out of range
>>
>> Anyone know why?
>>
>>
> Looks as if text2 didn't have 78 items.
> Put a print text2 after the line
>
> text2 = re.findall(r".*", text)
> print text2
>
> to have a look at it.
> Possibly (if working in IDLE for instance) you
> may write
>
>>>> text2 # or
>>>> len(text2)
>
> to get smoe information
>
> Gregor
>
> after the error-message to see what's the case
Sorry, I should have mentioned this before. I already tried that and there
is ~1300 items in text2 for all of the html files. This script actually
works without the indexing error if I get rid of the if section and rewrite
within the for section so that there is not another iteration call:
for i in range(l):
input = open(list[i], "rb")
text = input.read()
input.close()
text2 = re.findall(r".*", text)
text3 = string.join(text2[79:])
output = open("../Q/"+list[i], "wb")
output.write(text3)
output.close()
This works without a hitch and removed the proper material from each file
(over 140 files). The only reason why I wanted the if statement was so that
I could do an if:else test in case a file did not have the proper file size.
Any ideas?
Thanks.
SA
--
"I can do everything on my Mac I used to on my PC. Plus a lot more ..."
-Me