现在的位置: 首页 > 综合 > 正文

002_023 Python 跨平台读取无缓存字符

2018年02月14日 ⁄ 综合 ⁄ 共 387字 ⁄ 字号 评论关闭

代码如下:

#encoding=utf-8

print '中国'

#跨平台读取无缓存字符

try: #windows
    import msvcrt 
except ImportError: #unix
    def getch():
        import sys,tty,termios
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(fd)
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch
    
print 'Enter somthing'
c = msvcrt.getch()
print c

打印结果如下:后面为控制台打印

中国
Enter somthing

>>> c = msvcrt.getch()
>>>
>>> print c
o
>>>

抱歉!评论已关闭.