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

来源:互联网 发布:机械联结烙印矩阵 编辑:程序博客网 时间:2024/06/11 08:54

代码如下:

#encoding=utf-8print '中国'#跨平台读取无缓存字符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
>>>

0 0