How to get Linux console window width in Python

后端 未结 14 1373
清歌不尽
清歌不尽 2020-11-22 15:08

Is there a way in python to programmatically determine the width of the console? I mean the number of characters that fits in one line without wrapping, not the pixel width

14条回答
  •  鱼传尺愫
    2020-11-22 15:26

    Not sure why it is in the module shutil, but it landed there in Python 3.3, Querying the size of the output terminal:

    >>> import shutil
    >>> shutil.get_terminal_size((80, 20))  # pass fallback
    os.terminal_size(columns=87, lines=23)  # returns a named-tuple
    

    A low-level implementation is in the os module. Also works in Windows.

    A backport is now available for Python 3.2 and below:

    • https://pypi.python.org/pypi/backports.shutil_get_terminal_size

提交回复
热议问题