O
O
olerrz2021-09-13 17:11:44
Python
olerrz, 2021-09-13 17:11:44

Removing chat id from .txt?

Hello, I ran into a problem that the bot cannot remove the telegram id from the text editor.

The write function works like this

f = open('act.txt', 'a')
f.write(str(message.from_user.id) + '\n')

And it is working, the bot should automatically remove the id from the text editor at the end of certain actions. How can it be implemented?

There is such a working example but it is not automated.
with open("act.txt", "r") as f:
    lines = f.readlines()
  with open("act.txt", "w") as f:
    for line in lines:
      if line.strip("\n") != message.text:
        f.write(line)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jerwright, 2021-09-13
@jerwright

You can try like this:

import os
with open("act.txt", "r") as f:
    lines =  [line.replace('\n', '') for line in f.readlines() if message.from_user.id not in line] #Не добавляем выбранный пользователем id
with open("act.txt", "w") as j:
    for line in lines:
        if text not in line:
            j.write(line)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question