尝试使用时出现一个有趣的错误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
我尝试读取的文件为空。如何避免出现此错误,而是获取一个空变量?
不要关闭文件
—
Alkesh Mahajan
第一行
—
Magix
open(...).close()
是为了确保文件存在