1. 程式人生 > >pickle.load的時候出現EOFError: Ran out of input

pickle.load的時候出現EOFError: Ran out of input

原因:load的檔案為空,就會出現這種錯誤。

解決方案:1.如果是讀取單個檔案的話,一定要開啟檔案所在路徑然後開啟檔案,檢視檔案是否為空。有可能之前不是空檔案,但由於用pickle.load檔案時需要開啟檔案操作,可能在這個過程中把檔案內容清空了也未可知。

2.如果是批量操作檔案的話,可以丟擲異常,這樣就不會影響整個程式的執行。

        with open(filename, 'rb') as input_file:
                try:
                        return pickle.load(input_file)
                except EOFError:
                        return None

以上