Home>

borrowed this code:

@bot.command()
async def rm_token(ctx, tk):
    author= ctx.message.author
    a= str(author.id)
    if a in kinglist:
        f= open("token.txt","a+")
        d= f.readlines()
        f.seek(0)
        for i in d:
            if i != tk:
                f.write(i)
        f.truncate()
        f.close()
        await ctx.send(f'Token deleted successfully!')

It's supposed to remove the given token from token.txt, but it removes absolutely everything.

Here is an example:

he was supposed to remove only Lai6uy8_3hPFPwIxLh, but removed all tokens

I also tried to enter a token that does not exist at all, but it deleted all the tokens

Selasi2022-01-30 16:23:45
  • Answer # 1

    you can do that, but that's the first thing that came to my mind, I don't really like the fact that the file is opened twice, but it should work

    @bot.command()
    async def rm_token(ctx, tk):
        author= ctx.message.author
        a= str(author.id)
        if a in kinglist:
            f= open("token.txt", "r")
            old_token= f.readlines()
            f.close()
            with open("token.txt", "w") as file_obj:
              for token in old_token:
                token= token.strip()
                if token != tk:
                  file_obj.write(token + '\n')
    

    instead of opening twice, you can just set the resolution to a+

    Selasi2022-01-30 17:07:56

    I'm afraid not, I don't know for sure, but file.readlines() didn't work for me in a+ mode.

    maestro2022-01-30 17:19:23

    okay then, let it be so, there is nothing particularly terrible in this

    Selasi2022-01-30 17:21:46
  • Answer # 2

    you can do that, but that's the first thing that came to my mind, I don't really like the fact that the file is opened twice, but it should work

    @bot.command()
    async def rm_token(ctx, tk):
        author= ctx.message.author
        a= str(author.id)
        if a in kinglist:
            f= open("token.txt", "r")
            old_token= f.readlines()
            f.close()
            with open("token.txt", "w") as file_obj:
              for token in old_token:
                token= token.strip()
                if token != tk:
                  file_obj.write(token + '\n')
    

    instead of opening twice, you can just set the resolution to a+

    Selasi2022-01-30 17:07:56

    I'm afraid not, I don't know for sure, but file.readlines() didn't work for me in a+ mode.

    maestro2022-01-30 17:19:23

    okay then, let it be so, there is nothing particularly terrible in this

    Selasi2022-01-30 17:21:46
  • Answer # 3

    truncate I think it's superfluous here. + try to use with when creating files

    Doesn't answer the question. To leave comments or ask the author for clarifications, leave a comment on the relevant post. -from the check queue

    void2022-01-30 18:39:59
  • Answer # 4

    truncate I think it's superfluous here. + try to use with when creating files

    Doesn't answer the question. To leave comments or ask the author for clarifications, leave a comment on the relevant post. -from the check queue

    void2022-01-30 18:39:59