Python 3 pickle typeerror a bytes-like object is required not ‘str’

In this Python tutorial, we will discuss how to fix Python 3 pickle typeerror a bytes-like object is required not ‘str’ error.

Recently, I was trying to read a file using the pickle module in python. I got an error: TypeError: a bytes-like object is required, not ‘str’. Below is the code I used for reading a file.

import pickle              

file = open('student.p', 'r')
student = pickle.load(file)      
file.close()                       

print(student)

You can see the error screenshot, how the pickle typeerror a bytes-like object is required not ‘str’ error appearing.

pickle error
Python 3 pickle typeerror a bytes-like object is required not str

typeerror a bytes-like object is required not ‘str’ python 3

The solution is very simple, here we have to use ‘rb’ instead of ‘r’ in the below line of code:

file = open('student.p', 'rb')

The full code looks like below:

import pickle              

file = open('student.p', 'rb')
student = pickle.load(file)      
file.close()                       

print(student)

Now, when you execute the code, the error typeerror a bytes-like object is required not ‘str’ python 3 will not appear.

You may like the following Python tutorials:

Also, it will fix the below errors:

  • typeerror a bytes-like object is required not ‘str’ python 3
  • python 3 pickle typeerror a bytes-like object is required not ‘str’
  • typeerror a bytes-like object is required not ‘str’ python 3 split
  • python 3 replace typeerror a bytes-like object is required not ‘str’
  • python 3 csv typeerror a bytes-like object is required not ‘str’