Last updated: Apr 11, 2024
Reading time·4 min
The Python "Pickle EOFError: Ran out of input" occurs when the file you are trying to open and load is empty.
To solve the error, check if the size of the file is greater than 0 bytes or
handle the EOFError
exception.
Here is an example of how the error occurs.
import pickle with open('employees.txt', 'rb') as my_file: unpickler = pickle.Unpickler(my_file) employee_data = unpickler.load() print(employee_data)
The code sample assumes that you have an employees.txt
file that is empty.
One way to solve the error is to check if the file's size is greater than 0
bytes before using pickle.
import os import pickle file_path = 'employees.txt' if os.path.getsize(file_path) > 0: with open(file_path, 'rb') as my_file: unpickler = pickle.Unpickler(my_file) employee_data = unpickler.load() print(employee_data) else: print('The file is empty')
The os.path.getsize() method takes a file path as a parameter and returns the size of the file in bytes.
The if
statement checks that the file has a size of greater than 0
bytes (is
not empty).
The file in the example is empty, so the else
block runs.
Here is a more complete example that writes to a file and then reads from it using pickle.
import os import pickle file_path = 'employees.txt' employees = { 'name': ['Alice', 'Bobby Hadz', 'Carl'], 'age': [29, 30, 31] } with open(file_path, 'wb') as employees_file: pickle.dump(employees, employees_file) if os.path.getsize(file_path) > 0: with open(file_path, 'rb') as my_file: unpickler = pickle.Unpickler(my_file) employee_data = unpickler.load() print(employee_data) else: print('The file is empty.')
We first used the pickle.dump()
method to write the employees
dictionary to
the employees.txt
file and then used the pickle.Unpickler()
class to read
the pickle data stream.
wb
mode by mistakeA common cause of the error is opening the file in wb
or w
mode by mistake.
The w
(write) or wb
(write binary) modes truncate the file before opening
it.
In other words, if you open the file in wb
mode, its contents are deleted.
If you then try to open the file in rb
(read binary) mode, it will be empty
which will cause the "Pickle EOFError: Ran out of input" error.
For example, suppose you have the following code sample.
import pickle file_path = 'employees.txt' employees = { 'name': ['Alice', 'Bobby Hadz', 'Carl'], 'age': [29, 30, 31] } with open(file_path, 'wb') as employees_file: employee_data = pickle.load(employees_file)
The code sample causes the
io.UnsupportedOperation: not readable
error because we opened the file in wb
mode by mistake.
The wb
mode truncates the file (deletes its contents) and is used for writing
binary data to the file.
Instead, the rb
(read binary) mode should be used for reading the file as we
did in the previous subheading.
If the pickled file might be empty, you have to make sure that its size is
greater than 0
bytes before reading from it.
import os import pickle file_path = 'employees.txt' if os.path.getsize(file_path) > 0: with open(file_path, 'rb') as my_file: unpickler = pickle.Unpickler(my_file) employee_data = unpickler.load() print(employee_data) else: print('The file is empty')
EOFError
exception in a try/except blockYou can also solve the error by using a try/except
block.
import pickle file_path = 'employees.txt' employee_data = {} try: with open(file_path, 'rb') as my_file: unpickler = pickle.Unpickler(my_file) employee_data = unpickler.load() print(employee_data) except EOFError: print('An EOFError exception occurred. The file is empty')
We placed the with open
statement in a try
block.
If an error occurs, the except
block is run.
Using the with open()
statement should be your preferred approach over using
open()
directly because the file is closed even if an error occurs.
The except
block is run if an
EOFError is raised in the
try
block.
If an EOFError
exception is raised, then the pickled file is empty and you
need to handle the exception as you see fit.
For example, you could write to the file as we did in the first subheading.
Alternatively, you can set the outer scope variable to a default value in the
except
block.
import pickle file_path = 'employees.txt' employee_data = {} try: with open(file_path, 'rb') as my_file: unpickler = pickle.Unpickler(my_file) employee_data = unpickler.load() print(employee_data) except EOFError: print('An EOFError exception occurred. The file is empty') employee_data = {}
The previous examples used the with open()
statement, however, you can also
use the open()
function directly.
import pickle file_path = 'employees.txt' employees = { 'name': ['Alice', 'Bobby Hadz', 'Carl'], 'age': [29, 30, 31] } file_path = 'employees.txt' file_output = open(file_path, 'wb') pickle.dump(employees, file_output) file_output.close() file_input = open(file_path, 'rb') data = pickle.load(file_input) # 👇️ {'name': ['Alice', 'Bobby Hadz', 'Carl'], 'age': [29, 30, 31]} print(data) print(type(data)) # <class 'dict'> file_input.close()
We first opened the file in wb
(write binary) mode and used the
pickle.dump()
method to write the dictionary to the file.
Make sure to close the file when using the open()
function directly.
Otherwise, you'd have a memory leak in your application.
The next step is to open the file in rb
(read binary) mode.
The pickle.load() method reads the pickled representation of the file.
You can learn more about the related topics by checking out the following tutorials: