Fix Python Pickle Load TypeError: file must have ‘read’ and ‘readline’ attributes Error – Python Tutorial

By | August 25, 2019

When you are using python pickle library to load an exsiting file, which has saved a python object, you may find TypeError: file must have ‘read’ and ‘readline’ attributes error. In this tutorial, we will introduce how to fix this error and load python object successfully.

Here is an example:

car_obj_2 = pickle.load('binary_list.bin')

Where binary_list.bin is a file that has saved a python object. Then you will get this error: TypeError: file must have ‘read’ and ‘readline’ attributes.

python pickle TypeError - file must have 'read' and 'readline' attributes

Python pickle.load() function is defined as:

pickle.load(file, *, fix_imports=True, encoding="ASCII", errors="strict")

Where file is a file object, not a file name.

To fix this error, we should open a file then use pickle.load() function.

Here is a solution.

with open("binary_list.bin","rb") as f:
    car_obj_2 = pickle.load(f)

Then this error is fixed.

Leave a Reply

Your email address will not be published. Required fields are marked *