Pickle: TypeError: a bytes-like object is required, not 'str' [duplicate] Pickle: TypeError: a bytes-like object is required, not 'str' [duplicate] python python

Pickle: TypeError: a bytes-like object is required, not 'str' [duplicate]


You need to open the file in binary mode:

file = open(fname, 'rb')response = pickle.load(file)file.close()

And when writing:

file = open(fname, 'wb')pickle.dump(response, file)file.close()

As an aside, you should use with to handle opening/closing files:

When reading:

with open(fname, 'rb') as file:    response = pickle.load(file)

And when writing:

with open(fname, 'wb') as file:    pickle.dump(response, file)


In Python 3 you need to specifically call either 'rb' or 'wb'.

with open('C:\Users\Dorien Xia\Desktop\Pokemon-Go-Bot-Working-Hack-API-master\pgoapi\pgoapi.py', 'rb') as file:    data = pickle.load(file)


You need to change 'str' to 'bytes'. Try this:

class StrToBytes:    def __init__(self, fileobj):        self.fileobj = fileobj    def read(self, size):        return self.fileobj.read(size).encode()    def readline(self, size=-1):        return self.fileobj.readline(size).encode()with open(fname, 'r') as f:    obj = pickle.load(StrToBytes(f))