Python3:EOFError: Ran out of input
使用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部落格 ” ,連結:http://blog.itpub.net/2459/viewspace-2809964/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- python3 和 python2 input 功能的不同點Python
- POI2012RAN-Rendezvous
- 為什麼需要改變RAN的架構?架構
- $(":input")和$("input")區別
- delete input 與 delete all inputdelete
- input
- linux 中的 nohup 命令(設定後臺程式): nohup: ignoring input and appending output to ‘nohup.out’LinuxAPP
- Python -- raw_input() and input() -- ACMPythonACM
- OOM(Out Of Memory)OOM
- Out With the Old and in With the New
- java out of memoryJava
- out of springSpring
- 美高森美為LTE及5GC-RAN提供回程產品GC
- jquery 中$("form :input") $("form input") 區別jQueryORM
- raw_input() 與 input()的區別
- jQuery :inputjQuery
- <input> disabled
- OCP(11g)-----> oracle First In First Out (FIFO)/Last In First OutOracleAST
- SQL not exist out joinSQL
- OOM--OUT OF MEMORYOOM
- Kotlin 1.2.50 is out!Kotlin
- input和:input選擇器的區別
- ABAP 辨析ON INPUT|REQUEST|CHAIN-INPUT|CHAIN-REQUESTAI
- JavaScript input 事件JavaScript事件
- HTML input rangeHTML
- Resource is out of sync with the file system
- ref和out區別
- B. Two Out of Three
- Tape-out Checklist FormORM
- python中input()與raw_input()的區別Python
- backup archivelog delete input 與delete all input 區別Hivedelete
- rman 中的 delete all input 和 delete input 的區別delete
- angular input和outputAngular
- react input[type='number']React
- input捕捉Enter鍵
- div實現input
- HTML input 元素概述HTML
- HTML input date calendarHTML