You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

pickle加载报错‘Ran out of input’问题求助

解决Pickle加载时的EOFError: Ran out of input问题

问题详情

代码片段

scene_pt_path = str(kwargs.pop('scene_pt'))
print('loading scenes from %s' % (scene_pt_path))
with open(scene_pt_path, 'rb') as f:
    conn_matrixes = pickle.load(f)
    edge_matrixes = pickle.load(f)
    vertex_vectors = pickle.load(f)
    scene_descs = pickle.load(f)

报错信息

loading scenes from gen_files/val_questions.pt
<_io.BufferedReader name='gen_files/val_questions.pt'>
---------------------------------------------------------------------------
EOFError                                  Traceback (most recent call last)
<ipython-input-19-9b71b8a52590> in <module>
----> 1 run_validate(set_params)

1 frames
<ipython-input-16-f964f68cdb2c> in __init__(self, **kwargs)
     93             print(f)
     94             conn_matrixes = pickle.load(f)
---&gt; 95             edge_matrixes = pickle.load(f)
     96             vertex_vectors = pickle.load(f)
     97             scene_descs = pickle.load(f)

补充信息

  • 查看gen_files/val_questions.pt文件,在Atom中显示为二进制乱码
  • 生成该pickle文件时,是通过多次pickle.dump操作写入对象
  • 已尝试修改文件打开模式为rw、手动调用close(),均未解决问题

原因分析与解决方案

1. 生成文件时未写入足够对象

如果生成pickle文件时,只写入了少于4个对象,或者写入过程中断(程序崩溃、磁盘空间不足),加载时会因读到文件末尾仍未取够对象触发错误。

解决办法:
检查生成代码,确保按顺序完整写入4个对象。示例正确生成代码:

with open('gen_files/val_questions.pt', 'wb') as f:
    pickle.dump(conn_matrixes, f)
    pickle.dump(edge_matrixes, f)
    pickle.dump(vertex_vectors, f)
    pickle.dump(scene_descs, f)

如果生成时是一次性dump包含4个对象的元组,加载代码也要对应调整:

# 加载时改为一次读取元组
with open(scene_pt_path, 'rb') as f:
    conn_matrixes, edge_matrixes, vertex_vectors, scene_descs = pickle.load(f)

2. 文件损坏或不完整

文件可能在保存、传输过程中损坏,或生成时程序异常退出导致写入不完整。

解决办法:

  • 重新生成val_questions.pt文件,确保生成过程无报错、程序正常结束
  • 检查文件大小,若远小于预期值,说明未完整写入,必须重新生成

3. 打开模式错误

rw模式不适用于二进制文件,会导致文件指针位置异常,引发读取错误。

解决办法:
保持使用rb模式打开文件,with语句会自动管理文件关闭,无需手动调用close()。


验证排查步骤

可以逐步加载对象,定位问题节点:

with open(scene_pt_path, 'rb') as f:
    print("加载第一个对象:", pickle.load(f))  # 验证第一个对象是否正常
    print("加载第二个对象:", pickle.load(f))  # 验证第二个对象是否存在

如果第一个对象加载失败,说明文件本身损坏;如果第二个加载失败,说明生成时未写入第二个对象。

内容的提问来源于stack exchange,提问作者Malody Gamor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 21:40:16