Streaming text from a file while its been written to

498 Views Asked by At

Is there a better and cleaner way to stream data from a text file as it been written to in real time?

At the moment im using a server to simulate data being written continuously and using seek() method to only read the most recently added data and the print it to the screen.

The problem Im having is testing if the file is been written to or not and if its not then to sleep for a short period then test if its been written to then generate and output to print to the screen.

import time
seek1 = 198707
seek2 = 198719
file_open = open('C:\Users\Jay\Desktop\Tools\Uni-    Server\UniServerZ\core\\apache2\logs\\access.log')
#C:\Users\Jay\Desktop\Tools\Uni-Server\UniServerZ\core\apache2\logs
#C:\Users\Jay\PycharmProjects\Parsly\\access.log

def generate_list(read_lines):#properly build this with seek positional checks.
    #file_object.seek(seek_position)
    #lines = file_object.readlines()
    for i in read_lines:
        yield i


def seek_new_postion(generator):
    listt = []
    seek = file_open.tell()
    old_seek = None
    counter = 0
    while 1:
        if old_seek == seek and not counter == 5:
            print "sleeping"
            time.sleep(1)
            counter +=1
        else:
            p = generate_list(file_open.readlines())
            for i in p:
                print i
            time.sleep(1)
            print "___________________________"
            print "start seek: %s" % file_open.tell()
            print "old seek: %s" % old_seek
            print "___________________________"
            old_seek = seek
            seek = file_open.tell()
            counter = 0

print file_open.tell()
for i in seek_new_postion("test"):
print i
0

There are 0 best solutions below