gpt4 book ai didi

python - 泡菜 : TypeError: a bytes-like object is required, 不是 'str'

转载 作者:IT老高 更新时间:2023-10-28 20:31:16 33 4
gpt4 key购买 nike

当我在 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)

关于python - 泡菜 : TypeError: a bytes-like object is required, 不是 'str',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39146039/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com