在python中是否有一种方法以编程方式确定控制台的宽度?我指的是一行中不换行的字符数,而不是窗口的像素宽度。

Edit

寻找在Linux上工作的解决方案


当前回答

我正在尝试从这里调用stty大小的解决方案:

columns = int(subprocess.check_output(['stty', 'size']).split()[1])

然而,这对我来说失败了,因为我正在编写一个脚本,期望在stdin上重定向输入,stty会抱怨“stdin不是终端”。

我能做到这样:

with open('/dev/tty') as tty:
    height, width = subprocess.check_output(['stty', 'size'], stdin=tty).split()

其他回答

看起来代码有点问题,Johannes:

getTerminalSize需要导入操作系统 什么是env?看起来像os.environ。

另外,为什么要在返回之前切换线路和cols ?如果TIOCGWINSZ和stty都说行,那么cols,我说就这样吧。这让我困惑了10分钟,直到我注意到这种不一致。

Sridhar,我在管道输出时没有得到这个错误。我很确定它在尝试中被正确地捕获了——除了。

帕斯卡,“HHHH”在我的机器上不起作用,但“hh”可以。我很难找到这个函数的文档。它看起来依赖于平台。

chochem,合并。

以下是我的看法:

def getTerminalSize():
    """
    returns (lines:int, cols:int)
    """
    import os, struct
    def ioctl_GWINSZ(fd):
        import fcntl, termios
        return struct.unpack("hh", fcntl.ioctl(fd, termios.TIOCGWINSZ, "1234"))
    # try stdin, stdout, stderr
    for fd in (0, 1, 2):
        try:
            return ioctl_GWINSZ(fd)
        except:
            pass
    # try os.ctermid()
    try:
        fd = os.open(os.ctermid(), os.O_RDONLY)
        try:
            return ioctl_GWINSZ(fd)
        finally:
            os.close(fd)
    except:
        pass
    # try `stty size`
    try:
        return tuple(int(x) for x in os.popen("stty size", "r").read().split())
    except:
        pass
    # try environment variables
    try:
        return tuple(int(os.getenv(var)) for var in ("LINES", "COLUMNS"))
    except:
        pass
    # i give up. return default.
    return (25, 80)

我正在尝试从这里调用stty大小的解决方案:

columns = int(subprocess.check_output(['stty', 'size']).split()[1])

然而,这对我来说失败了,因为我正在编写一个脚本,期望在stdin上重定向输入,stty会抱怨“stdin不是终端”。

我能做到这样:

with open('/dev/tty') as tty:
    height, width = subprocess.check_output(['stty', 'size'], stdin=tty).split()

上面的代码在我的linux上没有返回正确的结果,因为winsize-struct有4个unsigned shorts,而不是2个signed shorts:

def terminal_size():
    import fcntl, termios, struct
    h, w, hp, wp = struct.unpack('HHHH',
        fcntl.ioctl(0, termios.TIOCGWINSZ,
        struct.pack('HHHH', 0, 0, 0, 0)))
    return w, h

HP和HP应该包含像素的宽度和高度,但不是。

从Python 3.3开始,它是直接的: https://docs.python.org/3/library/os.html#querying-the-size-of-a-terminal

>>> import os
>>> ts = os.get_terminal_size()
>>> ts.lines
24
>>> ts.columns
80

下面是一个应该与Linux和Solaris兼容的版本。根据来自madchine的帖子和评论。需要子流程模块。

def termsize():
    import shlex, subprocess, re
    output = subprocess.check_output(shlex.split('/bin/stty -a'))
    m = re.search('rows\D+(?P\d+); columns\D+(?P\d+);', output)
    if m:
        return m.group('rows'), m.group('columns')
    raise OSError('Bad response: %s' % (output))
>>> termsize()
('40', '100')