473,320 Members | 2,117 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,320 software developers and data experts.

blocking read on stdin on Windows?


I want to do a very simple thing in Windows. (Using Python Shell.)

I want to write a prompt to sys.stdout and read the user input.
(Ideally, without waiting for a newline.)

Here are the problems I'm encountering. Newbie problems, no doubt.

sys.stdin.read() gives me an attribute error
sys.stdin.readline() doesn't block waiting for input. And even
if it did, it would block waiting for a newline.

I thought sys.stdin was supposed to behave like a File object.
In Python Shell it's actually idlelib.rpc.RPCProxy, but that's
understandable. But shouldn't it support read()?

And without termios (unavailable on Windows), how would I
set the file to nonblocking read mode? PyWin32 doesn't seem to
have any help, but I might just have missed it.

I assume a programming language & system would have a pretty
simple way to get input from the user!

Thanks,
Jeff

PS: Python rocks. I've used it a lot, but so far only on UNIX,
and generally for filters or web scripts -- haven't needed to
ask any questions before.

Jul 18 '05 #1
5 5243
On Sat, 04 Sep 2004 11:23:12 -0400, Jeff Learman <jl******@cisco.com>
declaimed the following in comp.lang.python:

I want to do a very simple thing in Windows. (Using Python Shell.)

I want to write a prompt to sys.stdout and read the user input.
(Ideally, without waiting for a newline.)
Library reference
Section 22 (M$ specific)
Subsection .1.2 (Console I/O)

lib> kbhit( )
lib> Return true if a keypress is waiting to be read.
lib>
lib> getch( )
lib> Read a keypress and return the resulting character. Nothing is
echoed to the console. This call will block if a keypress is not already
available, but will not wait for Enter to be pressed. If the pressed key
was a special function key, this will return '\000' or '\xe0'; the next
call will return the keycode. The Control-C keypress cannot be read with
this function.
lib>
lib> getche( )
lib> Similar to getch(), but the keypress will be echoed if it
represents a printable character.
lib>
lib> putch( char)
lib> Print the character char to the console without buffering.
lib>
lib> ungetch( char)
lib> Cause the character char to be ``pushed back'' into the console
buffer; it will be the next character read by getch() or getche().

stdin tends to be buffered by the OS -- the OS doesn't release
anything until the new-line. You have to use OS specific operations to
get to the data in the buffer.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #2

(sheepish grin: didn't see the MS-specific chapter!)

Thanks :)

Dennis Lee Bieber wrote:
On Sat, 04 Sep 2004 11:23:12 -0400, Jeff Learman <jl******@cisco.com>
declaimed the following in comp.lang.python:

I want to do a very simple thing in Windows. (Using Python Shell.)

I want to write a prompt to sys.stdout and read the user input.
(Ideally, without waiting for a newline.)


Library reference
Section 22 (M$ specific)
Subsection .1.2 (Console I/O)

lib> kbhit( )
lib> Return true if a keypress is waiting to be read.
lib>
lib> getch( )
lib> Read a keypress and return the resulting character. Nothing is
echoed to the console. This call will block if a keypress is not already
available, but will not wait for Enter to be pressed. If the pressed key
was a special function key, this will return '\000' or '\xe0'; the next
call will return the keycode. The Control-C keypress cannot be read with
this function.
lib>
lib> getche( )
lib> Similar to getch(), but the keypress will be echoed if it
represents a printable character.
lib>
lib> putch( char)
lib> Print the character char to the console without buffering.
lib>
lib> ungetch( char)
lib> Cause the character char to be ``pushed back'' into the console
buffer; it will be the next character read by getch() or getche().

stdin tends to be buffered by the OS -- the OS doesn't release
anything until the new-line. You have to use OS specific operations to
get to the data in the buffer.


Jul 18 '05 #3

Hmm, getch() and getche() don't block.

The lib ref page says, "Read a keypress and return the resulting
character. Nothing is echoed to the console. This call will block if a
keypress is not already available, but will not wait for Enter to be
pressed." However:

import msvcrt

print msvcrt.kbhit()
print "prompt: ",
ch = msvcrt.getche()
print
print ord(ch)

Results -- without typing a key:

0
prompt:
255

Any ideas?

Thanks,
Jeff

Dennis Lee Bieber wrote:
On Sat, 04 Sep 2004 11:23:12 -0400, Jeff Learman <jl******@cisco.com>
declaimed the following in comp.lang.python:

I want to do a very simple thing in Windows. (Using Python Shell.)

I want to write a prompt to sys.stdout and read the user input.
(Ideally, without waiting for a newline.)


Library reference
Section 22 (M$ specific)
Subsection .1.2 (Console I/O)

lib> kbhit( )
lib> Return true if a keypress is waiting to be read.
lib>
lib> getch( )
lib> Read a keypress and return the resulting character. Nothing is
echoed to the console. This call will block if a keypress is not already
available, but will not wait for Enter to be pressed. If the pressed key
was a special function key, this will return '\000' or '\xe0'; the next
call will return the keycode. The Control-C keypress cannot be read with
this function.
lib>
lib> getche( )
lib> Similar to getch(), but the keypress will be echoed if it
represents a printable character.
lib>
lib> putch( char)
lib> Print the character char to the console without buffering.
lib>
lib> ungetch( char)
lib> Cause the character char to be ``pushed back'' into the console
buffer; it will be the next character read by getch() or getche().

stdin tends to be buffered by the OS -- the OS doesn't release
anything until the new-line. You have to use OS specific operations to
get to the data in the buffer.


Jul 18 '05 #4
On Sat, 04 Sep 2004 16:35:00 -0400, Jeff Learman <jl******@cisco.com>
declaimed the following in comp.lang.python:

Any ideas?
Seems to work here (W98SE, "MS-DOS PROMPT" window)...

G:\>type t.py
import msvcrt

print msvcrt.kbhit()
print "prompt: ",
ch = msvcrt.getche()
print
print ord(ch)

G:\>python t.py
0
prompt: a
97

G:\>python t.py
0
prompt:
0
Note -- the second one was <ctrl-@>; IOW, a "null" byte.

G:\>type t.py
import msvcrt

print msvcrt.kbhit()
print "prompt: ",
ch = msvcrt.getche()
print
print `ch`
G:\>python t.py
0
prompt:
'\x00'

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #5

Aha. It works for me too, if I run from DOS prompt.
It's only failing when I run from IDLE Python Development Environment.
I'll file a bug report.

Thanks for your help! I'd be happy to help you out in return;
just let me know if you want some piano or Hammond organ tracks
for original recordings ;)

Jeff

Dennis Lee Bieber wrote:
On Sat, 04 Sep 2004 16:35:00 -0400, Jeff Learman <jl******@cisco.com>
declaimed the following in comp.lang.python:
Any ideas?


Seems to work here (W98SE, "MS-DOS PROMPT" window)...

G:\>type t.py
import msvcrt

print msvcrt.kbhit()
print "prompt: ",
ch = msvcrt.getche()
print
print ord(ch)

G:\>python t.py
0
prompt: a
97

G:\>python t.py
0
prompt:
0
Note -- the second one was <ctrl-@>; IOW, a "null" byte.

G:\>type t.py
import msvcrt

print msvcrt.kbhit()
print "prompt: ",
ch = msvcrt.getche()
print
print `ch`
G:\>python t.py
0
prompt:
'\x00'


Jul 18 '05 #6

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

Similar topics

0
by: Arnau Sánchez | last post by:
Hello, I have a problem when reading from stdin (using it as pipe) in a Python program (the sender is written in C): C (sender) write(1, buffer, 4) Python (recv.py) sys.stdin.read(256) ...
3
by: Uwe Mayer | last post by:
Hi, I want two python programs to communicate over stdIO channels. The one executes the other via the popen3 function: amc = Popen3("./amc/amc.py", True, 0) line = stdin.readline()...
1
by: Uwe Mayer | last post by:
Hi, I use select() to wait for a file object (stdin) to become readable. In that situation I wanted to read everything available from stdin and return to the select statement to wait for more. ...
1
by: barr | last post by:
Hi Can any one help. I am trying to write a python scipt that takes input as args and/or as piped input ( possibly the output of another program). I want to read stdin ( the piped in stuuff...
4
by: Jonathan Fine | last post by:
Hello I have written a program that interacts with a command line program. Roughly speaking, it mimics human interaction. (With more speed and accuracy, less intelligence.) It works fine...
1
by: Jakob Bieling | last post by:
Hi, whenever I try to read input from stdin, the called function (ie. std::cin.read ()) blocks until my buffer is full. But I want to function to return immediately, regardless of having read...
7
by: hg | last post by:
Hi, Is there a way ? ... select ? hg
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
by: James Mills | last post by:
On Fri, Nov 7, 2008 at 8:54 AM, Thomas Christensen <thomasc@thomaschristensen.orgwrote: Check out circuits . It has a Component called Stdin which allows you to have non-blocking Standard...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.