[Tutor] IndexError:list index out of range

Joerg Woelke lumbricus@gmx.net
Mon, 17 Dec 2001 22:41:33 +0100


On Mon, Dec 17, 2001 at 10:02:52AM -0800, DavidCraig@pia.ca.gov wrote:
> 
> I am using Idle on WIN 98.  I have the following program:
> 
> ######################################################
> #split and interactively page a string or file of text;
> ######################################################
> 

[ snip ]

> if __name__=='__main__':
>     import sys                           #when run, not imported
>     more(open(sys.argv[1]).read(), 10)   #page contents of file on cmdline

If there is no sys.argv[1] it yields IndexError.

A construct like this may help you:
try:
	what_you_want_to_do()
except IndexError:
	print "Usage: <cmd> <arg>"

> 
> Every time I try to run it I get the following error message.
> 
> raceback (most recent call last):
>   File "C:/Python21/Practice/more.py", line 17, in ?
>     more(open(sys.argv[1]).read(), 10)   #page contents of file on cmdline
           ^^^^^^^^^^^^^^^^^
<hint> If this file does not exist or you
are not allowed to read it, your script will
yield an error too.

> IndexError: list index out of range
> 
> I am unable to find the problem.  How is this out of range and what does
> the error message mean?

You want to read about try/except

> 
> TIA
> 
> Dave
> 
> D. H. Craig, CSM

HTH, HAND
and Greetings J"o!

-- 
Absence makes the heart grow frantic.