Pickle Error:IndexError:列表索引超出范围

时间:2018-05-19 01:17:34

标签: python python-3.x passwords python-3.6 pickle

我正在制作一个简单的小操作系统内核,我实现了一个基本的(超级易受攻击的btw,它现在只在beta 1中)密码系统,它在运行后立即崩溃。所以我把它放在命令提示符下进行故障排除,我收到了很大的错误。

    C:\Users\Evan\Documents\pynix\3.4.3\beta>C:\Users\Evan\Documents\pynix\3.4.3\beta\beta1kernel.py
Traceback (most recent call last):
  File "C:\Users\Evan\Documents\pynix\3.4.3\beta\beta1kernel.py", line 11, in <module>
    password = pickle.load(infile)
  File "C:\Python2732\lib\pickle.py", line 1384, in load
    return Unpickler(file).load()
  File "C:\Python2732\lib\pickle.py", line 864, in load
    dispatch[key](self)
  File "C:\Python2732\lib\pickle.py", line 1171, in load_put
    self.memo[self.readline()[:-1]] = self.stack[-1]
IndexError: list index out of range

我不知道问题是什么,但是我在Python 3中使用Spyder并且该文件确实存在并且在同一目录中...代码如下:

import random
import pickle
infile = open("save",'rb')
password = pickle.load(infile)
infile.close()

passinput = input("Insert your password, if you do not have a password, create a save file named 'save' and type your desired password.")
if passinput != password:
    print("Password is incorrect, please try again")
    input()
else:
And the rest

2 个答案:

答案 0 :(得分:0)

您的代码中的问题可能是您没有提供save文件的正确路径。请执行以下操作

如果文件与脚本位于同一文件夹中,则

import os
import random
import pickle
infile = open(os.path.join(os.path.dirname(__file__),"save"),'rb')
password = pickle.load(infile)
infile.close()

如果文件位于其他位置,则提供文件的完整路径

import random
import pickle
infile = open("Give_complete path to the file",'rb')
password = pickle.load(infile)
infile.close()

答案 1 :(得分:0)

如果save是文件,则应声明其格式,例如save.pickle。如果它是一个目录,那么您应该给出完整路径和文件名及其格式,例如home/save/myfile.pickle