Python Forum
Try-except in while loop: error with closing program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Try-except in while loop: error with closing program
#1
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
Reply
#2
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.
Reply
#3
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?
Reply
#4
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
Reply
#5
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
Reply
#6
Lupin_III Wrote:I forgot to say that I'm using python 2.7.18
Just don't! Python 2 is dead.
Reply
#7
Yeah, now is a good time to update to Python 3.x usability will only get worse!
Reply
#8
yes, you're absolutely right. I should upgrade this job to python 3, but at the moment I can't...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Noob here. Random quiz program. Using a while loop function and alot of conditionals. monkeydesu 6 1,390 Sep-07-2022, 02:01 AM
Last Post: kaega2
  Closing a program using a G10 button MBDins 2 1,871 May-23-2021, 05:49 PM
Last Post: MBDins
  while loop not closing djwilson0495 4 2,258 Aug-27-2020, 06:33 PM
Last Post: deanhystad
  program error Dalpi 4 2,482 Apr-30-2020, 11:46 PM
Last Post: Dalpi
  please help me in loop i am trying to run this program in raspberry pi by sending msg saifullahbhutta 2 2,328 Jan-27-2020, 02:32 PM
Last Post: saifullahbhutta
  Com Error with macro within for loop due to creating new workbook in the loop Lastwizzle 0 1,362 May-18-2019, 09:29 PM
Last Post: Lastwizzle
  While loop won't restart program :( TheDovah77 4 4,219 Apr-04-2019, 07:37 PM
Last Post: TheDovah77
  Program gives error but doesn't say where the error is. FWendeburg 3 2,804 Mar-09-2019, 11:42 PM
Last Post: FWendeburg
  How to rerun the program in a loop with different input? bharaths 3 3,773 Nov-23-2018, 09:36 AM
Last Post: bharaths
  Closing a program oldDog 4 3,357 Jul-29-2018, 10:39 AM
Last Post: oldDog

Forum Jump:

User Panel Messages

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