Error notification method when calling python from windows GUI or winsound.Beep

What i did

I tried to make a sound only when an exception was raised in python of windows.

test_alert.py



import winsound

def hoge():
    #Some exception occurs

if __name__ == '__main__':
    try:
        hoge()
    except:
        winsound.Beep(400, 200)

As a mechanism, from 35.4. Winsound — Audio playback interface for Windows, when an exception occurs, a 200ms sound is played at 400Hz. Only. (Convenient because I didn't need additional pip install)

Background

In windows, a python script file may be started in a single shot like .bat from GUI such as Explorer. At that time, the shell window opens and the standard output is displayed only at the time of execution, and it closes when the processing is completed.

The problem is that even when the script terminates abnormally, the shell window closes and I can't see standard error output. (An error is displayed but the shell window closes immediately) Then, I do not know whether it is a normal end or an abnormal end.

I ran into this problem because I called the python script directly from Tablacus Exporer like this: Pass the selected item in Tablacus Explorer from JScript to python and rename it all at once --Qiita

What is good? Or what I wanted to do

In the first place,

--I want you to close the shell window after executing the python script (because it is troublesome to close it every time) --But I want you to keep displaying the error output without closing the shell window at the time of abnormal termination (because you do not know the abnormal termination as mentioned above)

However, I'm glad that it was the easiest to implement by investigating this and that,

So, I arrived at this mechanism.

If this makes a sound, I know that there is something wrong with it, so I thought I should open it as an IDE and debug it as I like.

(Bonus) If you run the script via cmd.exe

$ python hoge.py || rundll32 user32.dll,MessageBeep

Windows is also like linux, and you can pipe it with the execution result.

Reference: Beep with Python (Windows only) --Notes on numerical calculations (provisional)

Or is it possible to start the shell window with / k, receive the execution result with .bat and branch?

test.bat


call test.bat
if %errorlevel% == 1 (
    echo test.As a result of executing bat, 1 was returned as the return value
)

Reference: Cmd --DOS Command List --Programming Field Reference: [bat] How to write a Windows batch file (* .bat) Summary for myself --Qiita

that's all.

Recommended Posts

Error notification method when calling python from windows GUI or winsound.Beep
Precautions and error handling when calling .NET DLL from python using pythonnet
Python installation method Windows
Python from or import
Installation method when using RealSense from Python (pyenv edition)
Task registration or Notification in Python in Outlook (from VIM)
Import error resolution record when touching Python code from VSCode
Error when playing with python
Error log output method when using Bottle framework on GAE / Python
[python] Error when installing library ramkan
Error when building mac python environment
Tips for calling Python from C
Execute Python code from C # GUI
Python error detection run from Powershell
Precautions when using phantomjs from python
How to deal with OAuth2 error when using Google APIs from Python