【问题标题】:ValueError when executing os.popen(...)执行 os.popen(...) 时出现 ValueError
【发布时间】:2020-05-08 07:08:51
【问题描述】:

我想获取终端的宽度,打印某个元素直到行尾。但是在 Windows 中执行此命令时出现 ValueError:

rows, columns = os.popen('stty size', 'r').read().split()
ValueError: not enough values to unpack (expected 2, got 0)

在 Ubuntu 中,这可以完美运行,但在 Windows 中却不行。 我可以在那里做什么?

【问题讨论】:

    标签: python python-3.x windows operating-system system


    【解决方案1】:

    您可以使用以下行在 Windows 命令提示符中获取缓冲区大小

    rows = int(os.popen('mode con | findstr Lines','r').read().strip('\n').strip(' ').split(':')[1].strip(' '))
    columns = int(os.popen('mode con | findstr Columns','r').read().strip('\n').strip(' ').split(':')[1].strip(' '))
    

    【讨论】:

    • 当我尝试这个时,我得到IndexError: list index out of range
    • 你能分享你正在使用的代码吗,因为这些语句对我来说独立工作正常
    猜你喜欢
    • 1970-01-01
    • 2017-05-09
    • 2011-10-28
    • 2018-06-12
    • 1970-01-01
    • 2022-06-28
    • 2021-02-22
    • 2017-01-09
    • 2021-08-14
    相关资源
    最近更新 更多