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


Messages In This Thread
Try-except in while loop: error with closing program - by Lupin_III - Jul-03-2020, 09:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  problem program runs in a loop jasserin 0 876 May-18-2024, 03:07 PM
Last Post: jasserin
  Noob here. Random quiz program. Using a while loop function and alot of conditionals. monkeydesu 6 4,311 Sep-07-2022, 02:01 AM
Last Post: kaega2
  Closing a program using a G10 button MBDins 2 2,475 May-23-2021, 05:49 PM
Last Post: MBDins
  while loop not closing djwilson0495 4 3,261 Aug-27-2020, 06:33 PM
Last Post: deanhystad
  program error Dalpi 4 3,600 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 3,007 Jan-27-2020, 02:32 PM
Last Post: saifullahbhutta
  error message on program launch DJPY1175 0 2,213 Jun-10-2019, 08:40 AM
Last Post: DJPY1175
  Com Error with macro within for loop due to creating new workbook in the loop Lastwizzle 0 1,940 May-18-2019, 09:29 PM
Last Post: Lastwizzle
  While loop won't restart program :( TheDovah77 4 5,954 Apr-04-2019, 07:37 PM
Last Post: TheDovah77
  Program gives error but doesn't say where the error is. FWendeburg 3 3,711 Mar-09-2019, 11:42 PM
Last Post: FWendeburg

Forum Jump:

User Panel Messages

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