当我在python 3中运行以下代码时,我继续收到此错误:
fname1 = "auth_cache_%s" % username
fname=fname1.encode(encoding='utf_8')
#fname=fname1.encode()
if os.path.isfile(fname,) and cached:
response = pickle.load(open(fname))
else:
response = self.heartbeat()
f = open(fname,"w")
pickle.dump(response, f)
这是我得到的错误:
File "C:\Users\Dorien Xia\Desktop\Pokemon-Go-Bot-Working-Hack-API-master\pgoapi\pgoapi.py", line 345, in login
response = pickle.load(open(fname))
TypeError: a bytes-like object is required, not 'str'
我尝试通过编码功能将fname1转换为字节,但它仍然没有解决问题。谁能告诉我什么是错的?
答案 0 :(得分:28)
您需要以二进制模式打开文件:
file = open(fname, 'rb')
response = pickle.load(file)
file.close()
写作时:
file = open(fname, 'wb')
pickle.dump(response, file)
file.close()
另外,您应该使用with
来处理打开/关闭文件:
阅读时:
with open(fname, 'rb') as file:
response = pickle.load(file)
写作时:
with open(fname, 'wb') as file:
pickle.dump(response, file)
答案 1 :(得分:9)
在Python 3中,您需要专门调用“&#; rb'或者' 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)
答案 2 :(得分:2)
您需要将'str'更改为'bytes'。试试这个:
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))
答案 3 :(得分:1)
我一直回到这个堆栈溢出链接,所以我在下次找到它时发布了真正的答案:
PickleDB搞砸了,需要修复。
pickledb.py第201行
自:
simplejson.dump(self.db, open(self.loco, 'wb'))
为:
simplejson.dump(self.db, open(self.loco, 'wt'))
问题永远解决了。