当我在 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 转换为字节,但它仍然无法解决问题。有人能告诉我出了什么问题吗?
您需要以二进制模式打开该文件:
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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)