Avatar ·

Writing to a specific line in a file in Python

📁 question

I need to write data to a specific line in a large file of a couple of gigabytes. More specifically, I currently need to read a line, then process it, and then delete it from the text file.

I found a solution that involves reading the entire file into a variable, then making changes to the variable and rewriting everything, but for large amounts of data this is obviously not the best option.

Can this be solved more simply? If not, then the actual question is, how to write data to a specific line without rewriting the entire file?

Avatar ·

Getting the user ID is done using message.from_user.id. To get any information about the message, you need to ""access"" message, there you have a lot of information about the message itself - from whom, at what time, etc. I implemented a small example that displays the user ID in a reply message, or after clicking an Inline button

import loggingfrom aiogram import Bot, Dispatcher, executor, types logging.basicConfig(level=logging.INFO)bot = Bot(token = ""BOT TOKEN HERE"")dp = Dispatcher(bot)    @dp.message_handler(commands=['start'])
                            
                                Log in                            
                             to leave an answer                        

Blogs