Writing Error in program

J. Clifford Dyer jcd at sdf.lonestar.org
Tue Nov 20 22:42:53 CET 2007


On Tue, Nov 20, 2007 at 01:02:38PM -0800, koutoo at hotmail.com wrote regarding Writing Error in program:
> 
> I have a code that writes to 2 seperate files.  I keep getting a "list
> index out of range" error.  The strange part is that when checking the
> files that I'm writing too, the script has already iterated through
> and finished writing, yet the error stated implies that it hasn't?  So
> how can it be, that my script has written to the files, yet the error
> is stating that it hasn't made it through the script?  I'll have 15
> files that I have written to and the script will bog out at number
> 10?  Is python doing something I'm not seeing?  I printed everything
> that was written on the shell and it shows that it went through the
> script, so how can it still say there are a few files left to iterate
> through?

As others have mentioned, posting code would be very helpful.  Also, what you say doesn't sound right.  "List index out of range" does not mean there are a few files left to iterate through.  It means that you have a list somewhere and you are trying to access an index beyond the last list item.

So say you have the following list:

l=['a','b','c']

and you try to access each item in it with the following loop:

for x in range(4):
	print l[x]

You will get the following output.

a
b
c
Traceback (most recent call last):
  File "<stdin>", line 2, in ?
IndexError: list index out of range

in other words it will print l[0], l[1], and l[2], but then when it tries to print l[3], it will raise an IndexError, because there is no l[3].  This does not mean it still has files to process.  More likely, it means it has overshot the files it does have to process, but more likely still it has nothing to do with file access.  We can't help you diagnose that without a code sample, though.

Cheers,
Cliff






More information about the Python-list mailing list