我正在嘗試制作一些通過名為“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
提交
取消