IndexError: string index out of range

Alex Martelli aleaxit at yahoo.com
Thu Apr 12 04:14:36 EDT 2001


"michael" <mhyoung at valdosta.edu> wrote in message
news:mailman.987034096.12363.python-list at python.org...
    [snip]
>   if (buf[0] == EXCLUDE):

This assumes 0 is in-range as an index: it will not be
if buf is an EMPTY (0-length) string.

> learning purposes. This program works fine until it gets to the last
> line in the testfile and I get this error.

Right, because after the last line an empty string is returned.

> Can someone please tell me what I'm doing wrong? I've looked all over
> the net and in several books for a solution.

For example, just change this previously-quoted test to
    if buf.startswith(EXCLUDE):

THIS will work even when buf is the 0-length string.


Alex






More information about the Python-list mailing list