views:

98

answers:

2

I want to reproduce the behavior of the command CHOICE in DOS batch but with python.

raw_input requires the user to type whatever then press the ENTER/RETURN key. What I really want is for the user to press a single key and the script to continue from there.

+2  A: 

For Unix, it uses sys, tty, termios modules.

import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)

For Windows, it uses msvcrt module.

import msvcrt
ch = msvcrt.getch()

Source

Brian R. Bondy
A: 

A small utility class to read single characters from standard input : http://code.activestate.com/recipes/134892-getch-like-unbuffered-character-reading-from-stdin/

z33m