Read data in real time

Asked

Viewed 928 times

3

I’m trying to make a tool to read and send information (in real time) from a log to my screen.

So far I’ve been able to read everything and send the information line by line and send everything to the screen, follow the code.

import time

count = 0
while True:

    arquivo = ('LOG')
    arq = open(arquivo, 'r')
    texto = arq.readlines()
    arq.close()
    count += 1
    print(texto[count])
    time.sleep(5)

The problem is that the program may or may not take long to generate a new line in the log, and when it arrives at the last line, the problem closes with the error.

print(texto[count])
IndexError: list index out of range

How would I make the program, waiting for the new line to be inserted inside the log file?

2 answers

3


This example can help you. Read one line at a time until the end of the file. The Arq.readline() method returns 'while there are no new lines. In your code you were trying to read all the lines in each while iteration.

import time


def monitorar(path):
    with open(path, 'r') as arq:
        while True:
            nova_linha = arq.readline()
            nova_linha = nova_linha.replace('\n', '')
            if nova_linha:
                yield nova_linha
            else:
                time.sleep(1.0)

caminho_arquivo = 'teste.log'
for idx, linha in enumerate(monitorar(caminho_arquivo)):
    print("{:5d}: {}".format(idx, linha))

0

Excellent answer from Mr Fsola.

I have a suggestion to make it more pythonic. Replacing replace with strip, we reduce a line of code.

#Python3
import time

def monitorar(logfile='file.log'):
    with open(logfile) as file:
        while True:
            linha = file.readline().strip()
            if linha:
                yield linha
            else:
                time.sleep(.5)

if __name__ == '__main__':
    logfile = 'file.log'
    for item, linha in enumerate(monitorar(logfile)):
        #print(item, linha)
        #print('%s: %s' % (item, linha))
        print('{:6d}: {}'.format(item, linha))
  • Being basically the same solution, you could suggest the change through a comment. When you have enough reputation points, you can use them. Until then, try to publish only concrete answers to the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.