Python script that alerts me when specific file gets created I wrote a small script that opens a window using PyQt when a specific file gets created.

This is to let me know when another program (that I have no control over) has finished calculating something. I know at the end of the calculation a specific file is created.

Here is the code:

from PyQt5.QtWidgets import QWidget, QLabel
import os.path

file_to_check = "D:/test.txt"

alert_widget = QWidget()
alert_widget.setWindowTitle("Program X finished")
done_label = QLabel("Done",alert_widget)
done_label.move(50,50)

file_does_not_exist = True

while file_does_not_exist:
    if os.path.isfile(file_to_check):
        file_does_not_exist = False

alert_widget.show()


The widget that gets shown luckily just pops up in front of whatever window im currently active in, whether it's browsing the internet or working on an excel sheet. That is intended behaviour, but I did not write it in by specifying it as a "top level" window or somehting.

The script is started in an IPython console in Spyder and just left to run there. It works exactly as expected.

Now my questions


is this constantly running loop good practice?
are there any downsides to having that loop constantly running in the background?
could it block other programs?
is there another, more elegant way to achieve what I'm looking to do?
is the code following python coding conventions?
The variable file_does_not_exist is simply used as loop control, but we don't need it:

while not os.path.isfile(file_to_check):
    pass


However, this loop is very inefficient.  It's what we call a busy loop; that is, it never pauses, but continually polls for its condition.  This is harmful, as it monopolises a processor and wastes energy.  Sensible operating systems will de-prioritise busy tasks like this (to reduce its impact on other processes), which will reduce the UI responsiveness when you exit the loop, until the scheduler catches up with the change in the program's behaviour.

Instead, we should use operating system features (preferably abstracted in a platform-independent Python library) to wait for a change, allowing your process to sleep until it's notified.  I don't know of a platform-independent library, but there are some possibilities for Linux (inotify) and Windows (watchdog) at least.  Using one of these will yield processor time to other processes and make your program into a "good citizen" on a shared system.