I'm trying to make something that goes through a file called "tokens.txt" and removes all of the invalid discord user tokens. However, instead of deleting them, it keeps writing the same invalid tokens in the file and messing it up. I don't know why it is not properly deleting the tokens. Please let me know how to fix this. The code is below.
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")