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

--------------------- 本文來自 匠人_C 的CSDN 博客 ,全文地址請點擊:https://blog.csdn.net/castle_cc/article/details/78193942?utm_source=copy 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM