在尝试使用Unpickler.load()
时,我遇到了一个有趣的错误,以下是源代码:
open(target, 'a').close()
scores = {};
with open(target, "rb") as file:
unpickler = pickle.Unpickler(file);
scores = unpickler.load();
if not isinstance(scores, dict):
scores = {};
以下是追踪信息:
Traceback (most recent call last):
File "G:\python\pendu\user_test.py", line 3, in <module>:
save_user_points("Magix", 30);
File "G:\python\pendu\user.py", line 22, in save_user_points:
scores = unpickler.load();
EOFError: Ran out of input
我正在尝试读取的文件为空。 如何避免出现此错误,并获得一个空变量?
open(...).close()
是为了确保文件存在。 - Magixos.path.isfile(target)
呢? - DLAN