在python中尝试无缓冲读取时出现奇怪的换行符
我有以下代码:在python中尝试无缓冲读取时出现奇怪的换行符,python,terminal,python-3.x,Python,Terminal,Python 3.x,我有以下代码: def getch(self): if os.name == 'posix': fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: tty.setraw(fd) ch = sys.stdin.read(1) finally: termios.tcsetattr
def getch(self):
if os.name == 'posix':
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)
elif os.name == 'nt':
ch = msvcrt.getch()
return ch
这在Python2.6和2.7上运行得很好,但是每当我尝试在Python3.0上测试它时,stdin.read调用就会打印出一行新行,我认为这可能是因为Python3更改为sys.stdin、stdout和stderr,但我不确定如何修复它
编辑:在OSX10.6.4Python3.1和Ubuntu9.04Python2.6上运行时,我遇到了这种情况。这可能是平台特有的问题。您是否在不同的基于POSIX的操作系统(例如Linux、BSD、Darwin等)上尝试过该代码。你的结果是一样的吗?它们处理终端操作的方式都略有不同,因此您可能需要考虑的不仅仅是
posix
与nt
之间的差异,还需要更深入一些 我试着在Ubuntu9.04上运行代码,问题完全一样,只是在Ubuntu2.6上也出现了问题。