The code below does the following, it reads a log file in real time and every time the line of the code has the letter N a beep emits from the windows.
At first this code is working.
To understand what I'm doing, I access a machine via putty, so I record a log of the session.
In this machine there are several sensors connected, so when a sensor is triggered on the screen it changes the status to ON, so when it turns OFF.
The code works every time the status is ON.
Every time it changes the status is included a line in the log, because it is a log of the putty session.
So if I play "turn-off" the beep goes with it.
But I would like if the last status, ie if last line read with the status ON the beep was active. Until q between a new line in the file with FF (from OFF).
I'm trying to get the FF to compare FF in the condition, I tried to use While .. but the beep lasts only the time it was programmed.
Does anyone qualify?
import time
import winsound
def monitorar(path):
with open(path, 'r') as arq:
while True:
nova_linha = arq.readline()
nova_linha = nova_linha.replace('\n', '')
if 'N' in nova_linha:
yield nova_linha
frequency = 2500 # Set Frequency To 2500 Hertz
duration = 1000 # Set Duration To 1000 ms == 1 second
winsound.Beep(2500, 1000)
else:
time.sleep(0.0)
caminho_arquivo = 'C:/loop/putty.log'
for idx, linha in enumerate(monitorar(caminho_arquivo)):
print("{:5d}: {}".format(idx, linha))