Python: beep on code completion

I am in a situation where my code takes a very long time, and I do not want to look at it all the time, but I want to know when it will be done.

How can I make (Python) code like an β€œalarm” sound when it is done? I was thinking about getting him to play a WAV file when it reaches the end of the code ...

Is that even a real idea? If so, how can I do this?

+55
python audio alarm
May 15 '13 at 19:04
source share
10 answers

In windows

import winsound duration = 1000 # millisecond freq = 440 # Hz winsound.Beep(freq, duration) 

Where the frequency is the frequency in Hz and the duration in milliseconds.

On Linux (and Mac)

 import os duration = 1 # second freq = 440 # Hz os.system('play --no-show-progress --null --channels 1 synth %s sine %f' % (duration, freq)) 

To use this example, you must install sox .

On Debian / Ubuntu / LinuxMint, you need to run in your terminal:

 sudo apt install sox 

Here is the macports way to do it ... run this your terminal:

 sudo port install sox 

Speech on Mac

And something is really cool, if you use a Mac in a terminal, you can probably do the same on Windows, but I only know for mac only, it will tell you that it is done:

 import os os.system('say "your program has finished"') 

It's about Linux

 import os os.system('spd-say "your program has finished"') 

You need to install the speech-dispatcher package on Ubuntu (or the corresponding package on other distributions):

 sudo apt install speech-dispatcher 
+91
May 15 '13 at 19:23
source share

This one seems to work with both Windows and Linux * ( from this question ):

 def beep(): print "\a" beep() 

On Windows, you can put an end to:

 import winsound winsound.Beep(500,1000) where 500 is the frequency in Herz 1000 is the duration in miliseconds 

*: to work with Linux, you may need the following (from a QO comment):

  • in the terminal, type 'cd / etc / modprobe.d', then 'gksudo gedit blacklist.conf'
  • comment out the line labeled "blacklist pcspkr", then reload
  • also check that the terminal settings have the "Terminal Bell" flag.
+20
May 15 '13 at 19:13
source share

You can use ubuntu speech manager:

 import subprocess subprocess.call(['speech-dispatcher']) #start speech dispatcher subprocess.call(['spd-say', '"your process has finished"']) 
+15
Apr 12 '15 at 14:42
source share
  print('\007') 

plays a sound of a call

+13
May 13 '15 at 16:32
source share

Kuchi's answer did not work for me on OS X Yosemite (10.10.1). I found a command that works that you can just call from Python. This works regardless of whether the terminal’s ringing is turned on even without a third-party library.

 os.system('afplay /System/Library/Sounds/Sosumi.aiff') 
+5
Feb 20 '15 at 18:05
source share

See: Python Sound ("Bell")
It helped me when I wanted to do the same.
All loans go to gbc

Quote:

You tried:

 import sys sys.stdout.write('\a') sys.stdout.flush() 

This works for me here on Mac OS 10.5

Actually, I think that your original attempt also works with a slight modification:

 print('\a') 

(You just need single quotes around a sequence of characters).

+4
May 15 '13 at 19:17
source share

This can be done using code as follows:

 import time time.sleep(10) #Set the time for x in range(60): time.sleep(1) print('\a') 
+3
Jun 30 '17 at 20:02
source share
 import subprocess subprocess.call(['D:\greensoft\TTPlayer\TTPlayer.exe', "E:\stridevampaclip.mp3"]) 
+1
May 2 '16 at 12:36
source share

Why use python? You can forget to delete it and check in the repository. Just run the python command with && and another command to trigger an alert.

 python myscript.py && notify-send 'Alert' 'Your task is complete' && paplay /usr/share/sounds/freedesktop/stereo/suspend-error.oga 

or drop the function into your .bashrc. I use apython here, but you can override 'python'

 function apython() { /usr/bin/python $* notify-send 'Alert' "python $* is complete" paplay /usr/share/sounds/freedesktop/stereo/suspend-error.oga } 
+1
Sep 28 '16 at 16:53
source share

I assume that you need a standard system call, and you do not want to worry about frequencies and duration, etc., you just need a standard window call.

 import winsound winsound.MessageBeep() 
+1
Apr 04 '17 at 21:42 on
source share



All Articles