0

I'm currently writing a program in python on Ubuntu. I have the requirement of reading log file.The log file is updating continually.My requirement is to read only updating list.For example first log file contain log file:

abc1
abc2
abc3

and after some time i update log file with

abc4
abc5.
abc6 and so on..

So every time we have to read only the updated list( like abc4 and abc5..). So to avoid the more memory i just want read only updated list instead of reading whole log file in Python.I am not getting the logic.I just write to read last two lines in log file.please help.

Python Code:

import time
templog = 'file name'
while 4:
    with open(templog,'r') as f:
        logstr = f.readlines()
        print(f.readline())
    if f:
        current_line = f.readlines()
    for line in logstr:
        previous_line = current_line
        current_line=line
    print(previous_line)
    print(current_line)
time.sleep(1)
sisanared
  • 4,175
  • 2
  • 27
  • 42
Tirumala
  • 49
  • 9
  • 1
    Remember which line you last read, skip to that line and then read from there. As this is a text file, you can not immediately seek to the desired position. You need to start from the beginning each time. – Selçuk Cihan Jan 24 '17 at 11:55
  • If the log file is to big you can rename it and archive. The new logs should go to new empty file. – Jerzy Pawlikowski Jan 24 '17 at 11:57
  • Did you check http://stackoverflow.com/questions/12523044/how-can-i-tail-a-log-file-in-python or http://stackoverflow.com/questions/1703640/how-to-implement-a-pythonic-equivalent-of-tail-f ? – fredtantini Jan 24 '17 at 12:09
  • Thank You Selcuk cihan for your suggestion. I tried to find out position.but could n't. Can you provide the logic(program) for your idea.. – Tirumala Jan 24 '17 at 13:40

0 Answers0