I am working on a Python project and need help with the following funciton. The
ID: 3823654 • Letter: I
Question
I am working on a Python project and need help with the following funciton. The whole program reades an input file line by line and writes to an output file according to the commands read in from the input file.
The command I am working on is pop, where it takes off the last line written to said output file.
In other words if I wrote to the output file:
5
yellow
blue
And if the next line read in from the input file was pop the output file would have to read:
yellow
blue
The code I have written so far opens the input file in read mode and the output file in write mode. The confusion being if the output file is in write mode how do I remove the last line written to this file. I thought about making a function poplastline that opens the output file in read mode and deletes the top line and rewrites it back into the file without it but this seems inefficient and I feel like opening the file twice in two different modes without closing either of them is going to cause problems. Any ideas how to accomplish this task without reopening the file in read mode? Or is that the correct way to do it?
Explanation / Answer
Seems like there is gap in understanding if not am suggesting other thing also.
The command I am working on is pop, where it takes off the last line written to said output file.
If it is last line written, then line should be blue and not 5.
You can do this.
Now you can continue to process your input file.
Hope this help.
===================================
If you want to remove top line from file, then I will advice to keep data in a stack and write at end of code. Or first write to a file in above manner and at last read the written file an dthen write in writting first line format.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.