0

I am trying to update a logfile continuously and I am accessing that logfile in log_parser.py file like below:

****log_parser.py****
import time
last_pos=0
while True:
    f=open(r".\log_file.log","r")
    f.seek(last_pos)
    for i in f.readlines():
        print i
        print "****************************"
    else:
        last_pos=f.tell()
        time.sleep(1)
        continue

Now I am trying to print log lines continuously. If there are no log lines it will record last position value and I seek() that position each iteration, but that gives me the wrong output.

****log_file.log****
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
.
.
.
.
.
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

I am appending those lines dynamically into that logfile.

 ****Excpected output*************
 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 ***********************
 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
 *************************
 cccccccccccccccccccccccccccccc
 **************************
 and upto zzzzzz

but now I am getting

  ***I am Getting************
  aaaaaaaaaaaaaaaaaaaaaaaaaaaa
  **********************
  bbbbbbbbbbbbbbbbbbbbbbbbbbbbb
  *********************
  cccccccccccccccccccccccccccccccccc
  ************************
  dddddddddddddddd
  *************************
  dddddddddddd
  *******************
  eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
  ***********************************
  upto zzzzz

but in between f.tell() method returns the pointer and f.seek() method seeks the position of middle of the line.

Can anyone please help solve this?

martineau
  • 119,623
  • 25
  • 170
  • 301
surya
  • 21
  • 6
  • Suggest you [edit] your question and use different sample data. It's difficult to tell the original lines from those appended with what you currently have. Also (don't know if it matters) I noticed you never close the file you open. Suggest you add that or use a `with` statement to have it done automatically. – martineau Sep 15 '16 at 09:49
  • I am just reading the file right ? then what is the need for close a file – surya Sep 15 '16 at 09:53
  • Every `open()` uses up some resources and your code is doing it a _lot_, plus you're having weird problems...just say'in. – martineau Sep 15 '16 at 09:57
  • Sorry don't know how to fix your code. However the answers to this question [_How to implement a pythonic equivalent of tail -F?_](http://stackoverflow.com/questions/1703640/how-to-implement-a-pythonic-equivalent-of-tail-f) may be helpful. – martineau Sep 15 '16 at 10:18

0 Answers0