Writing Error in program

K

koutoo

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?
 
L

Looney, James B

There could be any number of issues in your code that could cause that
problem.
Can you post some of the code in question?
 
K

kyosohma

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?

Could you post some of the code and the traceback? It would be
especially good to see your list and make sure you're iterating
through it correctly. Do you open the file in the loop, write to it
and close it?

How can it write to 15 files and then quit at number 10? That
statement seems to contradict itself.

More details for the group would be great. Thanks!

Mike
 
J

J. Clifford Dyer

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top