Python Forum

Full Version: Try-except in while loop: error with closing program
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I have this code that includes a sort of watch dog within a try block inside a while loop
I don't understand two things:
1) why sometimes, not always, it is generated the error;
2) why, because of this error, the program get closed and exits instead of going ahead with the while loop
This simple script should run forever (while loop) and the file my_time.txt is a simple file where another python program write last time its code is executed.
This is the code that write the timestamp in my_time.txt every 15 seconds
import time
with open(my_time.txt,"w") as Time_check:
    Time_check.write(str(time.time()))
this is the code I need help
import time
while True:
        try:
            with open(my_time.txt,"r") as Time_check:
                print(Time_check.read())
                Time_value=float(Time_check.read()) #This line generates the error
            Delta_time = (time.time()-Time_value)
            if Delta_time > 30: #30 seconds
                do FOO
            else:
                do FOO_
        except:
            print (sys.exc_info()[0])
            print (sys.exc_info()[1])
            print (sys.exc_info()[2])
This is the error that sometimes get generated
Error:
<type 'exceptions.ValueError'> could not convert string to float: <traceback object at 0x0000000003A70348>
Last time I got this error, the timestamp written inside the my_time.txt was 1593707454.53
Thanks a lot to all
I don't know why you have this error but the approach of opening the same file again and again at a high frequency while another program now and then modifies the file seems fundamentally wrong to me.

I just want to suggest 3 alternative approaches
  1. You could use the watchdog module available in pypi. Simply create an observer that watches the directory containing file.txt and does something every time the file is modified. I think this is the best strategy because you are reusing a robust code that was specially designed for this use case.
  2. You could also check the modification time of the file before opening it, thus avoiding many file openings.
  3. You could add a small sleep() operation in the loop to lower the frequency of your system calls.
Thanks for the suggestions, I'll try.
yes, for the sleep it's already set. I forgot to post it
But why exit the program instead of continuing with the loop cycle?
Hey buddy,

I tried this. Works every-time. I'm still looking at the reason your code failed. i'll update when i find it.

I would definitely look at adding a sleep function. While loops shouldn't loop infinitely for jobs like this.

 

import time

while True:
    with open(my_time.txt, "r") as Time_check:
        Time_value=float(Time_check.read())  # This line generates the error
        print(Time_value)
    Delta_time = (time.time() - Time_value)
    if Delta_time > 30:  # 30 seconds
        pass
    else:
        pass
Thanks.
Since yesterday I tryed the code on another PC, identical to the first one (PC embedded with windows 10) and apparently only on one PC I get the error. So it seems in some way linked to the PC...
I forgot to say that I'm using python 2.7.18
Lupin_III Wrote:I forgot to say that I'm using python 2.7.18
Just don't! Python 2 is dead.
Yeah, now is a good time to update to Python 3.x usability will only get worse!
yes, you're absolutely right. I should upgrade this job to python 3, but at the moment I can't...