原因:load的文件爲空,就會出現這種錯誤。
解決方案:
1.如果是讀取單個文件的話,查看文件是否爲空。
有可能之前不是空文件,但由於用pickle.load文件時需要打開文件操作,可能在這個過程中把文件內容清空了也未可知。
import pickle
import os
file_name = 'tokenizer.pkl'
if os.path.getsize(file_name):
with open(file_name,'rb') as f:
tokenizer = pickle.load(f)
else:
print('the file is 空')
2.如果是批量操作文件的話,可以拋出異常,這樣就不會影響整個程序的運行。
try:
with open(file_name,'rb') as f:
tokenizer = pickle.load(f)
except EOFError:
print('the file is 空')
如果不知道是什麼錯誤
try:
with open(file_name,'rb') as f:
reload_tokenizer = pickle.load(f)
except Exception as e:
print('the file is 空 %s'%e)