[Tutor] How do I fix this IndexError?

Simon Gerber nequeo at gmail.com
Tue Dec 20 23:37:33 CET 2005


Hi Nathan,

> 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

As you can see from the traceback, the error is occuring entirely in
the pickle module. Which probably means your pickle file is corrupted.
Or that you're trying to load your old exch.txt file which wasn't in
pickle format.

Simply delete 'exch.txt' and try again. Although you'll notice your
program still doesn't work, because it tries to load 'exch.txt' even
if there isn't an exch.txt.

You should consider rewriting your program so that it checks to see if
an 'exch.txt' file exists. If it does, it unpickles it. If not, it
uses your default 'rates' variables.

Speaking of rates, you have two typos in your code which you also need to fix.

> exch = pickle.load(store)

Should be

rates = pick.load(store)

And similarly,

>         pickle.dump(exch, store)

pickle.dump(rates, store)

Otherwise you're not saving or loading anyhing!

Cheers,

--
Seen in the release notes for ACPI-support 0.34:

'The "I do not wish to discuss it" release
  * Add workaround for prodding fans back into life on resume
  * Add sick evil code for doing sick evil things to sick evil
    screensavers'


More information about the Tutor mailing list