Project help? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Project help?

So I am working on this data entry project in which I have to enter data as follows:- Group 1- A B Group 2- X Y And so on, Also new data will entered periodically. Since I am using Python the program I came up with is to open the file in read mode save all the data in a list add new data in the list(such as C inside group 1) Then again open the file in write mode Write the entire data onto the file using the list. But the thing is that it seems wastage of resources as deleting the entire thing and writing it again every time I add a new entry. So is there any other way?

8th Dec 2020, 5:26 AM
Akash
Akash - avatar
2 Answers
+ 1
The solution you came up with, is practical and the normal way to do this. It is not possible to just 'insert' something in the middle of a file, you would have to re-write it again, at least starting from the point of the first change. A little improvement could be to open the file only once, in r+ mode, and after reading the content you can use seek() to reset the file pointer to the beginning and then you can do the write. A few variations of this pattern you can find here https://stackoverflow.com/questions/10507230/insert-line-at-middle-of-file-with-JUMP_LINK__&&__python__&&__JUMP_LINK
8th Dec 2020, 9:36 PM
Tibor Santa
Tibor Santa - avatar
+ 1
Tibor Santa thanks this will help
9th Dec 2020, 2:25 AM
Akash
Akash - avatar