Форум сайта python.su
Привет.
Для чтения по символьно из консоли я использую следующий код.
cli.py
import sys
class _Getch:
"""Gets a single character from standard input. Does not echo to the screen."""
def __init__(self):
try:
self.impl = _GetchWindows()
except ImportError:
self.impl = _GetchUnix()
def __call__(self):
ch = self.impl()
if ord(ch) == 127:
ch = '\x08'
sys.stdout.write('\x08\x20')
return ch
class _GetchUnix:
def __init__(self):
import tty, sys
self.text = ''
self.rch = ''
def __call__(self):
import sys, tty, termios, select
fd = sys.stdin.fileno()
fout = sys.stdout.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
class _GetchWindows:
def __init__(self):
import msvcrt
def __call__(self):
import msvcrt
return msvcrt.getch()
getch = _Getch()
#!/usr/bin/python
#coding:utf-8
from cli import getch
import sys
while True:
ch = getch.__call__()
sys.stdout.write(ch)
if ord(ch) == 90: break
Офлайн