command CHOICE in DOS batch replacement/reproduction in python
I want to reproduce the be开发者_如何学Pythonhavior 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.
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
A small utility class to read single characters from standard input : http://code.activestate.com/recipes/134892-getch-like-unbuffered-character-reading-from-stdin/
精彩评论