I'm trying to read from stdin character by character with a Python script, using
r, w, e = select.select([sys.stdin], , )
print "Read: %s" % sys.stdin.read(1)
/tmp$ python test.py
AAAA # I type in a few chars then ENTER.
Read: A # Only one char is detected :(
BBBB # I type in more characters
Read: A # Now all the previous characters are detected!?
Read: # ('\n' is read which I want)
Read: B # Wait, where are the other Bs?
CCCC # I need to type more chars so that BBB is printed, and the first C.
select.select is unaware of the buffering in a Python file object (such as
sys.stdin). You can bypass the file object entirely, though this will interact strangely with attempts to read from
import select import os import sys while True: r, w, e = select.select([sys.stdin], , ) print os.read(sys.stdin.fileno(), 1)