[Tutor] List and Indexing error.

Scot W. Stevenson scot@possum.in-berlin.de
Wed, 14 Aug 2002 10:48:09 +0200


Hi SA, 

> for i in range(l):
>     input = open(list[i], "rb")
>     text = input.read()
>     input.close()
>     text2 = re.findall(r".*", text)
>     if text2[78] == "<hr>":

If I understand this correctly, text is the content of a single file, and 
text2 is the result of some search. To me, it looks like one of your files 
in the directory is so short that after you have fed it thru the 
re.findall, there isn't anything at index 78 anymore.

How about wrapping the if part in a try construct to catch IndexErrors and 
then print the i (or even better, the name of the file itself with 
list[i]) and then see what is wrong with file 78? Something like (this is 
untested):

try:
    if text2[78] == "<hr>":
        <...>
except IndexError:
    print "IndexError occurred in %s file (index %s)" % (list[i], i)

That should give you the file that is causing the problem.

Y, Scot

-- 
Scot W. Stevenson wrote me on Wednesday, 14. Aug 2002 in Zepernick, Germany  
       on his happy little Linux system that has been up for 1376 hours       
        and has a CPU that is falling asleep at a system load of 0.02.