473,399 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Is there a simple way to exit a while loop on keystroke?

I am new to python, and have written a simple program to read a port
via telnet. I would like it to run until any key is pressed. Of
course I wouldn't mind requiring a specific keystroke in the future,
but I would think this is simpler for now.

I have used kbhit() and getch() many times in C, but I can't find
anything similar in Python. I am using Linux also, so the msvcrt
code isn't an option. I have tried sys.stdin.read(), but that hangs
UNTIL a key is pressed.

Thanks

Aug 31 '07 #1
8 20401
Hello,
I am new to python, and have written a simple program to read a port
via telnet. I would like it to run until any key is pressed. Of
course I wouldn't mind requiring a specific keystroke in the future,
but I would think this is simpler for now.

I have used kbhit() and getch() many times in C, but I can't find
anything similar in Python. I am using Linux also, so the msvcrt
code isn't an option. I have tried sys.stdin.read(), but that hangs
UNTIL a key is pressed.
You might want to look at http://docs.python.org/lib/module-curses.html

Another solution is to ask the user to hit CTRL-C
from time import sleep

try:
while 1:
print "BEEP"
sleep(1)
except KeyboardInterrupt:
print "BYE BYE"

HTH,
--
Miki <mi*********@gmail.com>
http://pythonwise.blogspot.com

Aug 31 '07 #2
Thanks,
The curses library doesn't look to helpful to me. However using CTRL-
C is fine and is working nicely.

BTW, it should be "time.sleep(1)" in the example above, instead of
just
"sleep(1)" (Just in case any other newbies like me read this)

Thanks again

Aug 31 '07 #3
On Aug 31, 7:11 pm, gsxg <rha...@gmail.comwrote:
Thanks,
The curses library doesn't look to helpful to me.
And yet it is.

--
Arnaud

Aug 31 '07 #4
On Aug 31, 11:11 am, gsxg <rha...@gmail.comwrote:
Thanks,
The curses library doesn't look to helpful to me. However using CTRL-
C is fine and is working nicely.

BTW, it should be "time.sleep(1)" in the example above, instead of
just
"sleep(1)" (Just in case any other newbies like me read this)

Thanks again
Depends on how you import 'time'

import time
time.sleep(1)

from time import sleep
sleep(1)

~Sean

Aug 31 '07 #5
On Aug 31, 3:55 pm, Arnaud Delobelle <arno...@googlemail.comwrote:
On Aug 31, 7:11 pm, gsxg <rha...@gmail.comwrote:
Thanks,
The curses library doesn't look to helpful to me.

And yet it is.

--
Arnaud
Maybe the OP is on Windows. The docs seem to indicate that the curses
module isn't for Windows (see excerpt below):

<quote>
While curses is most widely used in the Unix environment, versions are
available for DOS, OS/2, and possibly other systems as well. This
extension module is designed to match the API of ncurses, an open-
source curses library hosted on Linux and the BSD variants of Unix.
<unquote>
See also: http://docs.python.org/lib/module-curses.html

Oddly enough, I have it in my Windows distro, so it's rather
confusing. I've never used it, so I don't know if it plays nice on
Windows or not.

Mike

Aug 31 '07 #6
In message <11*********************@r29g2000hsg.googlegroups. com>, gsxg
wrote:
I am new to python, and have written a simple program to read a port
via telnet. I would like it to run until any key is pressed.
Did you mean "telnet" or did you mean "local terminal"? For a local
terminal, the following demo script should give you a starting point:

#!/usr/bin/python

import sys
import select
import termios
import tty

timeout = 0.0 # nonzero to wait that long for keypress

save_attrs = termios.tcgetattr(sys.stdin.fileno())
# save terminal settings for restoration--note that this script
# doesn't currently trap CTRL/C, so settings will not be properly
# restored if that is hit
tty.setcbreak(sys.stdin.fileno())
# or can use setraw to block CTRL/C
while True :
(input_ready, _, _) = select.select((sys.stdin,), (), (), timeout)
if sys.stdin in input_ready :
break
#end if
sys.stdout.write("Running...\n")
#end while
termios.tcsetattr(sys.stdin.fileno(), termios.TCSAFLUSH, save_attrs)
sys.stdout.write("Finished.\n")

Aug 31 '07 #7
"gsxg" <rh****@gmail.comwrote:
I am new to python, and have written a simple program to read a port
via telnet. I would like it to run until any key is pressed. Of
course I wouldn't mind requiring a specific keystroke in the future,
but I would think this is simpler for now.

I have used kbhit() and getch() many times in C, but I can't find
anything similar in Python. I am using Linux also, so the msvcrt
code isn't an option. I have tried sys.stdin.read(), but that hangs
UNTIL a key is pressed.
Unblock the stdin using the fcntl module. Then you get an IOError
if there is nothing.

def unblock(f):
"""Given file f , sets it unblock flag to true"""
fcntl.fcntl(f.fileno(),fcntl.F_SETFL,os.O_NONBLOCK )

f is the file object you get from the open...

hth - Hendrik

Sep 1 '07 #8
On Aug 31, 10:28 pm, kyoso...@gmail.com wrote:
On Aug 31, 3:55 pm, Arnaud Delobelle <arno...@googlemail.comwrote:
On Aug 31, 7:11 pm, gsxg <rha...@gmail.comwrote:
Thanks,
The curses library doesn't look to helpful to me.
And yet it is.
--
Arnaud

Maybe the OP is on Windows. The docs seem to indicate that the curses
module isn't for Windows (see excerpt below):
Ah yes, Windows! I didn't think about it. Yes, I reckon it's unlikely
that curses is implemented for MS Windows.

[...]
--
Arnaud
Sep 1 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Redduck | last post by:
Hello everyone. I am frustrated, I have written the simple program below for a class and I am having problems with the DO-WHILE loop. On the first run through the loop, everything works well, the...
15
by: PagCal | last post by:
Is this language missing the functionality of a C/C++ 'continue' statement? For example: While NOT isEof() If condition ' a C or C++ continue would work here ' but we are forced to use a...
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
0
by: adriann | last post by:
I have a strange problem whereby an attempt at a looped SQL query only passes successfully once. I get the error.. Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result...
9
by: Soneji | last post by:
Hello yet again! I seem to be having a problem with a logical OR inside a while loop. Basically, I've got it taking user input for a string, and I want them to type 'exit' to... well, exit. ...
9
by: somenath | last post by:
Hi All, I have doubt regarding how compiler understands about while loop. For example the bellow mentioned code produce the output as mentioned bellow. #include<stdio.h> int main(void) {
23
by: lisp9000 | last post by:
I wrote a small test program to read a file of data and print each line, but it's only printing the 2nd line out of 3 total lines. The test file, "foo.txt", has 3 lines: 7388: Zn->Z0 Run...
3
by: Rhals | last post by:
hey guys, I'm new to programming so please bare with me. My problem is that how do i scan/read a blank space? My WHILE loop terminates by typing "exit" no probs there, but when I need to keep the...
2
by: John Hutchison | last post by:
Hey everybody, this is my first post, so I hope I'm doing this right. I'm just starting to learn how to program, so I'm sorry if my error is obvious, but I can't seem to find the answer. I...
4
by: Haris Radoncic | last post by:
I picked up the "C Programming Language" and have been learning it bit by bit but Im alittle confused about EOF. I example 1-8, we need to write a program that outputs the number of tabs, blanks, and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.