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?

18th Feb 2017, 7:45 PM
guest013
guest013 - avatar
2 Respostas
+ 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()
18th Feb 2017, 8:17 PM
ASNM
ASNM - avatar
+ 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
19th Feb 2017, 7:15 AM
visph
visph - avatar