A completely silly question

Craig Ringer craig at postnewspapers.com.au
Sun Dec 19 02:17:06 EST 2004


On Sat, 2004-12-18 at 00:40, Amir Dekel wrote:
> This must be the silliest question ever:
> 
> What about user input in Python? (like stdin)
> Where can I find it? I can't find any references to it in the documentation.

Under UNIX, I generally either use curses, or just put the terminal into
raw mode:

.>>> def sane():
....    os.system("stty sane")
....
.>>> def raw():
....    os.system("stty raw")
....
.>>> raw()
.>>> x = sys.stdin.read(1)
                          a
.>>> sane()
.>>> x
'a'

but that's more for the benefit of others here, since you're on Windows.
Needless to say this isn't portable.

It can often be worth also using the 'echo' and 'noecho' arguments to
stty to prevent characters getting echoed in ugly places. If you do much
of this, it's probably worth just using curses, but if you have a fairly
basic app that just needs to read raw characters sometimes this approach
should be fine.

--
Craig Ringer





More information about the Python-list mailing list