Beeping the console

J

Jeffrey Barish

What is the easiest way to beep the console from a Python program (i.e.,
to do the same thing as echo -e \a)?
 
P

Peter Kleiweg

Jeffrey Barish schreef:
What is the easiest way to beep the console from a Python program (i.e.,
to do the same thing as echo -e \a)?

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

or if you don't mind a newline:

print '\a'
 
W

Wojciech Mula

Jeffrey said:
What is the easiest way to beep the console from a Python program (i.e.,
to do the same thing as echo -e \a)?

import os
import sys

os.write(sys.stdout.fileno(), '\007')

w.
 
J

Jeffrey Barish

Peter said:
Jeffrey Barish schreef:


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

or if you don't mind a newline:

print '\a'
I tried this solution before posting my question, but I tried it in the
Idle Python shell. When I run the command there it outputs \a. Can
anyone explain why? I should have tested it from a Python prompt in a
normal window, but I wasn't aware that Idle sometimes gives a different
response.

BTW, it's worth noting that in my program this solution works better
with

sys.stdout.flush()

immediately after the write.

Thanks to everyone for the responses.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top