0
Reading last 3 lines of text file
Ive wrote script which generating some data and save it in a txt file. Second script will compare last 3 lines of text file and compare are they equall. Can you guide me how do this?
2 Respuestas
+ 2
files=open("text.txt","w")
for i in range(5):
files.write("hi{0}\n".format(i+1))
files.close()
files=open("text.txt","r")
n=files.read().split("\n")
for j in range(len(n)-4,len(n)):
print(n[j])
files.close()
+ 2
If your file isn't too big, you can whole read it at once, as suggested by @ASNM...
But if your file grow quickly ( depending on frequence of appending new data ), you would be advised to use the readline() method:
https://www.tutorialspoint.com/JUMP_LINK__&&__python__&&__JUMP_LINK/file_readline.htm