【问题标题】:Strange newline when attempting unbuffered reading in python尝试在python中进行无缓冲读取时出现奇怪的换行符
【发布时间】:2010-10-29 21:03:31
【问题描述】:

我有这个代码:

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

这在 python 2.6 和 2.7 上运行得很好,但是每当我尝试在 python 3.0 上测试它时,stdin.read 调用就会打印出一个新行,我认为这可能是因为 python 3 更改为 sys.标准输入、标准输出和标准错误,但我不知道如何修复它

编辑:在 OS X 10.6.4 python 3.1 和 Ubuntu 9.04 python 2.6 上运行这发生在我身上。

【问题讨论】:

    标签: python terminal python-3.x


    【解决方案1】:

    这可能是特定于平台的问题。您是否在不同的基于 POSIX 的操作系统(例如 Linux、BSD、Darwin 等)上尝试过代码。你的结果一样吗?它们处理终端操作的方式都略有不同,因此您可能需要考虑的不仅仅是 posixnt 并更深入一点。

    【讨论】:

    • 我尝试在 ubuntu 9.04 上运行代码,但问题完全相同,只是它也出现在 ubuntu 上的 python 2.6 上。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 2020-03-04
    相关资源
    最近更新 更多