3
The code below does the following, it reads a log file in real time and every time in the line of the code has the letter N a beep is issued from windows.
At first this code is working.
To understand what I’m doing, I access a machine via Putty, so I record a session log.
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 disables OFF.
The code works, every time the status gets paged.
Every time it changes the status a line is included in the log, as it is a log of the Putty session.
So if I keep playing "on-off" the beep accompanies.
But I would like if the last status, ie if the last line deals is with the status ON the beep was active. Up to q between a new line in the file with FF(from OFF).
To catch this part, rs.. I tried to invert the IF to compare FF in the condition, I tried to use While.. but the beep only lasts 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))
That one
with
and all that is beneath it until thetime.sleep(0.0)
should not be indented with 4 more characters?– Victor Stafusa
I do not understand, I only know if you copy and paste this code the way it is works
– Lucas Oliveira
humm has already solved its problem ?
– ederwander