Why does Win 7 make a beep when I run a Python script?

94 views Asked by At

I have seen a few question asking how to make a Python script that makes a beep sound.

This question is not about that.

When I run certain Python scripts Windows 7 makes a beep sound but I want to stop the beep.. or at least understand why it's happening.

Here is a test script - test.py:

#!/usr/bin/python

# importing modules
import os
import time
import datetime
from datetime import datetime

def f_log(strtext):
# In this method try and write to the log file
    logFilePath = '/home/osmc/python/pyscripter_file.txt'
    ts_local = datetime.now()
    ts = ts_local.strftime("%d/%b/%Y %H:%M:%S")

    try:
        # Check if the logfile exists
        if(os.path.isfile(logFilePath)):
            # Append the error message to the log file. 'a+' creates the file if it does not exist.
            with open(logFilePath, 'a+') as f:
                f.write(ts + '\t' + strtext + '\n')
        else:
            print("Log File does not exist - Creating File now")
            with open(logFilePath, 'a+') as f:
                f.write(ts + '\t' + 'Log File does not exist - Creating File now' + '\n')
                f.write(ts + '\t' + strtext + '\n')
    except IOError as e:
        # Handle the exception
        print("Error Msg")

f_log("Test Script")
print("Hello World")

This script is in a directory on a Raspberry Pi running OSMC... in a folder called /home/osmc/python I have opened it as a remote file in the Pyscripter IDE and can see ssh://osmc//home/osmc/python/test.py So this means, the Pyscripter IDE, running on Windows 7, is running the Python script on the remote machine. When I run the script this way I get a beep when it finishes executing.

If I create a crontab to run it in osmc...

PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/bin/python
53 22 * * * /usr/bin/python /home/osmc/python/test.py >> out.txt  2>&1

Then the script also gives a beep at the end of execution.

If I run the script from a command line to osmc (from an ssh connection in Putty)

# cd /home/osmc/python
# python test.py

I DO NOT get a beep when it completes.

If I run this next script in Pyscripter (or any other way) it does NOT make a beep.

import sys
print('Python '+sys.version.replace('\n','')+' at '+sys.executable+' on '+sys.platform)

Why do I sometimes get the beep? Something in the Python code in the first script I guess?

Flex

1

There are 1 answers

0
FlexMcMurphy On

My test Python script writes to a log file. I had that log file open in PyScripter IDE.

PyScripter IDE by design makes a beep to signal a File Change Notification affecting any files open in the IDE. This is what was causing the beep whether I was running the script from within PyScripter or not I was still getting the beep because the script was writing to the file open in PyScripter IDE.

I asked the developer and it is currently not possible to turn off this beep.

Flex