I am trying to open a pickle file in Python 3 with code that worked in Python 2 but is now giving me an error. Here is the code:
with open(file, 'r') as f:
d = pickle.load(f)
TypeError Traceback (most recent call last)
<ipython-input-25-38f711abef06> in <module>()
1 with open(file, 'r') as f:
----> 2 d = pickle.load(f)
TypeError: a bytes-like object is required, not 'str'
I saw on other SO answers that people had this problem when using open(file ,'rb')
and switching to open(file ,'r')
fixed it. If this helps, I tried open(file ,'rb')
just to experiment and got the following error:
UnpicklingError Traceback (most recent call last)
<ipython-input-26-b77842748a06> in <module>()
1 with open(file, 'rb') as f:
----> 2 d = pickle.load(f)
UnpicklingError: invalid load key, '\x0a'.
When I open the file with f = open(file, 'r')
and the enter f
I get:
<_io.TextIOWrapper name='D:/LargeDataSets/Enron/final_project_dataset.pkl' mode='r' encoding='cp1252'>
So I also tried:
with open(file, 'rb') as f:
d = pickle.load(f, encoding='cp1252')
and got the same error as with using 'rb':
UnpicklingError Traceback (most recent call last)
<ipython-input-27-959b1b0496d0> in <module>()
1 with open(file, 'rb') as f:
----> 2 d = pickle.load(f, encoding='cp1252')
UnpicklingError: invalid load key, '\x0a'.