开发者

command CHOICE in DOS batch replacement/reproduction in python

开发者 https://www.devze.com 2022-12-24 01:58 出处:网络
I want to reproduce the be开发者_如何学Pythonhavior of the command CHOICE in DOS batch but with 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/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号