1

我正在尝试使用以下代码从泡菜文件中加载用户点。

import pickle
filehandle = True
try:
    pickle_file = open("points.mvm", 'r')
except:
    filehandle = False
if filehandle:
    points = pickle.load(pickle_file)
    pickle_file.close()
else:
    points = 0

但是,它引发了EOFerror. 该文件肯定在那里,而且似乎也有内容。

4

1 回答 1

0

这应该有效:

import pickle

points = 0

try:
    with open('points.mvm', 'rb') as pickle_file:
        points = pickle.load(pickle_file)
except IOError:
    pass

print points

points += 1

with open('points.mvm', 'wb') as pickle_file:
    pickle.dump(points, pickle_file)

请注意,如果您的目录中仍有该points.mvm文件,则需要将其删除,因为它可能已损坏(即不是二进制文件),并且会为您提供EOFError.

于 2014-05-06T23:02:48.223 回答