我正在尝试制作一些通过名为“tokens.txt”的文件并删除所有无效的不和谐用户令牌的东西。但是,它并没有删除它们,而是一直在文件中写入相同的无效标记并将其弄乱。我不知道为什么它没有正确删除令牌。请让我知道如何解决这个问题。代码如下。import requests with open("tokens.txt","r+") as f: for line in f: token=line.strip("\n") headers = {'Content-Type': 'application/json', 'authorization': token} url = "https://discordapp.com/api/v6/users/@me/library" r=requests.get(url,headers=headers) if r.status_code == 200: print(token+" is valid") else: print("invalid token found "+token) tokenlines=f.readlines() for usertoken in tokenlines: if usertoken.strip("\n") != token: f.write(usertoken) print("invalid token automatically deleted")
1 回答
阿波罗的战车
TA贡献1862条经验 获得超6个赞
您可以找到所有有效令牌,将它们收集到列表中,然后使用有效令牌完全重写文件。
import requests
valid_tokens = []
with open("tokens.txt","r+") as f:
for line in f:
token=line.strip("\n")
url = "https://discordapp.com/api/v6/users/@me/library"
r = requests.get(url, headers=headers)
if r.status_code == 200:
print(token + " is valid")
valid_tokens.append(token)
else:
print("invalid token found "+token)
# Could collect the invalid tokens here
with open("tokens.txt","w+") as f:
for i in valid_tokens:
f.write(i+"\n")
添加回答
举报
0/150
提交
取消