• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: How to save only 5 lines in file with python code?

Resolved: How to save only 5 lines in file with python code?

0
By Isaac Tonny on 15/03/2023 Issue
Share
Facebook Twitter LinkedIn

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 device
The value that save is like
or
But every time I save a new value, the old value is deleted. I nedd to modify the def to make it save only 5 values, and when I save value number 6 it takes the place of number 1 and value number 7 takes the place of 2 and value number 8 takes the place of 3 … etc ?!!

Best 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

python
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Unable to run Docker .NET 6 console image

02/04/2023

Resolved: Input Focus between two React Components

02/04/2023

Resolved: linq2db throws exception when filtering by nested collection

02/04/2023

Comments are closed.

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.