使用pickle.load(f)加載pickle文件時,報錯:EOFError: Ran out of input.
可能緣由:文件爲空。
解決辦法:加載非空文件。
其餘解決辦法:
一、加載前判斷文件是否爲空ide
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()
二、捕獲異常.net
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=1code