Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
some logic help
#4
That's odd. I ran this program:
with open("output.txt", "r") as file:
    for last_line in file:
        pass
    while True:
        for new_line in file:
            if 'Driver not connected' in new_line or 'Data stale' in new_line:
                print("Starting the driver")
Then I opened the file "output.txt", typed 'Driver not connected', and saved the file. The program promptly prints "Starting the driver" one time. I wait a while to see if this is printed again. It does not. Only once. So I append 'Driver not connected' to the output.txt and save the file. The program promptly prints "Starting the driver" one time.

I get the same behavior if I run this program.
# Get pointer to end of file
with open("output.txt", "a") as file:
    fp = file.tell()
 
while True:
    with open("output.txt", "r") as file:
        file.seek(fp)
        for line in file:
            if 'Driver not connected' in line or 'Data stale' in line:
                print("Starting the driver")
            fp += len(line)
Each program only prints the line once when I add a line to the file. What happens if you run the program on your computer? Does it behave the same?
Reply


Messages In This Thread
some logic help - by droidus - Jun-10-2023, 05:34 PM
RE: some logic help - by deanhystad - Jun-10-2023, 08:53 PM
RE: some logic help - by droidus - Jun-10-2023, 11:34 PM
RE: some logic help - by deanhystad - Jun-11-2023, 07:55 AM
RE: some logic help - by Gribouillis - Jun-11-2023, 09:28 AM
RE: some logic help - by droidus - Jun-11-2023, 10:41 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020