我正在尝试从文件中获取腌制列表,但我不断收到错误。是否需要在每个函数中从同一个文件中解压相同的对象,或者解压一次就可以了?
这是我尝试的最后一件事:
import pickle, sys
def openFile(fileName, mode):
"""Open a file."""
try:
file = open(fileName, mode)
except IOError as e:
print("Unable to open the file", fileName)
print(e)
input("Press the enter key to exit.")
sys.exit()
else:
return file
def writeScore(file, score):
"""Write score to file."""
try:
highScores = pickle.load(file)
except IOError as e:
print("File doesn't exist.")
print(e)
except EOFError as e:
print("File is empty.")
print(e)
else:
highScores.append(score)
highScores = highScores.sort()
pickle.dump(highScores, file)
score_file = openFile("highScores.dat", "ab+")
writeScore(score_file, 1000)
score_file.close()
score_file = openFile("highScores.dat", "rb")
highScore = pickle.load(score_file)
print(highScore)