In this post, we will see how to resolve How to save only 5 lines in file with python code?
Question:
I have this function def to save some variable value I have add it manually by remote control of some E2 deviceBest Answer:
Some assumptions to be declared:- Age in the file is determined from the top down. The first line is the oldest, and the last line is the newest. Any new lines are always placed at the end of the file.
- You’re only writing one line at a time.
- No threading or multiple processes are involved in this.
In this case, the flow is simple.
- Open the file to get all lines.
- Add your line.
- If the number of lines exceeds your cap, then remove the first line.
- Write all lines out.
The code looks like this:
If you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com