ITPub博客

首页 > IT职业 > IT生活 > Python3:EOFError: Ran out of input

Python3:EOFError: Ran out of input

IT生活 作者:solution 时间:2021-09-09 09:27:03 0 删除 编辑

使用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 {}


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2459/viewspace-2809964/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-05

  • 博文量
    101
  • 访问量
    21774