发布时间:2019-09-28 08:38:33编辑:auto阅读(2604)
使用pickle.load(f)加载pickle文件时,报错:EOFError: Ran out of input. 
可能原因:文件为空。 
解决办法:加载非空文件。
其他解决办法: 
1、加载前判断文件是否为空
import os
scores = {} # scores is an empty dict already
if os.path.getsize(target) > 0:      
    with open(target, "rb") as f:
        unpickler = pickle.Unpickler(f)
        # if file is not empty scores will be equal
        # to the value unpickled
        scores = unpickler.load()
2、捕获异常
open(target, 'a').close()
scores = {};
try:
    with open(target, "rb") as file:
        unpickler = pickle.Unpickler(file);
        scores = unpickler.load();
        if not isinstance(scores, dict):
            scores = {};
except EOFError:
    return {}
转自:http://blog.csdn.net/castle_cc/article/details/78193942?locationNum=4&fps=1
上一篇: Mac同时安装python2和pytho
下一篇: Python3快速入门(十)——Pyth
 51255
 50693
 41291
 38112
 32573
 29476
 28338
 23199
 23166
 21493
 1568°
 2287°
 1896°
 1833°
 2148°
 1879°
 2568°
 4303°
 4157°
 2963°