edit a file line by line interactively from user input in python
Question: I want to know how to edit a file on the fly row by row in python.

For example I have a text file where I usually have:

    
    
    key value
    key value
    key value
    key value
    key value
    ...
    

they are not necessarily the same pair for each line. It's just the way I
explained it.

I would like to show line by line key and value (on my terminal) and then I
want to do one of this two things:

-just press enter (or whatever hot-key) to go ahead and read (show) next line.

-enter a new value then hit enter. this will actually replace the value (that was being shown) on the file and finally go ahead to show next pair of key values.

Till end of file or possibly till I type 'quit' or some other keyword. doesn't
matter.

-Being able to go back to the previous row would be a plus (in case of accidentally going to next row), but it's not too important for now.

I find myself often editing huge files in a very tedious and repetitive way,
and text editors are really frustrating with their cursors going everywhere
when pressing the arrow-key. Also having to use the backspace to delete is
annoying.

I know how to read a file and how to write a file in python. But not in such
interactive way. I only know how to write the whole file at once. Plus I
wouldn't know if it is safe to open the same file in both reading and writing.
Also I know how to manipulate each line, split the text in a list of values
etc... all I really need is to understand how to modify the file at that exact
current line and handle well this type of interaction.

what is the best way to do this?


Answer: The way I would go about this is to load all the lines of the text file in a
list, and then iterate through that list, changing the values of the list as
you go along. Then at the very end (when you get to the last line or whenever
you want), you will write that whole list out to the file with the same name,
so that way it will overwrite the old file.

