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
#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


Messages In This Thread
RE: Try-except in while loop: error with closing program - by Gribouillis - Jul-03-2020, 10:00 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  problem program runs in a loop jasserin 0 873 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,303 Sep-07-2022, 02:01 AM
Last Post: kaega2
  Closing a program using a G10 button MBDins 2 2,473 May-23-2021, 05:49 PM
Last Post: MBDins
  while loop not closing djwilson0495 4 3,260 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,006 Jan-27-2020, 02:32 PM
Last Post: saifullahbhutta
  error message on program launch DJPY1175 0 2,206 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,938 May-18-2019, 09:29 PM
Last Post: Lastwizzle
  While loop won't restart program :( TheDovah77 4 5,952 Apr-04-2019, 07:37 PM
Last Post: TheDovah77
  Program gives error but doesn't say where the error is. FWendeburg 3 3,704 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