[Tutor] List and Indexing error.
Gregor Lingl
glingl@aon.at
Wed, 14 Aug 2002 08:26:36 +0200
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
>Thanks.
>SA
>
>
>