[Tutor] How do I fix this IndexError?

Alan Gauld alan.gauld at freenet.co.uk
Tue Dec 20 23:41:56 CET 2005


I haven't read your code in detail but from the error message:

> Traceback (most recent call last):
>  File "D:\Python24\exchange.py", line 28, in -toplevel-
>    exch = pickle.load(store)
>  File "D:\Python24\lib\pickle.py", line 1390, in load
>    return Unpickler(file).load()
>  File "D:\Python24\lib\pickle.py", line 872, in load
>    dispatch[key](self)
>  File "D:\Python24\lib\pickle.py", line 1207, in load_appends
>    mark = self.marker()
>  File "D:\Python24\lib\pickle.py", line 888, in marker
>    while stack[k] is not mark: k = k-1
> IndexError: list index out of range

The exception is happeming deep inside the picklle library so 
that usually indicates that you are not calling pickle properly 
- maybe a wrong type of data in the argument?

> store = open('exch.txt', 'rb')#load
> exch = pickle.load(store)

Have you checked that the exch.txt file is in fact a valid pickle file?
And that a readable binary file is what pickle.load() expects - from 
memory I think it is - so I'd check that exch.txt is a valid pickle file.

And check that its the right exch.txt - are there two files of that name 
that you might be picking up the wrong name for example?

Sorry, no definitive answer just some ideas for you to check out.

Alan G.

More information about the Tutor mailing list