How to get Linux console window width in Python

后端 未结 14 1144
清歌不尽
清歌不尽 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:40

    Starting at Python 3.3 it is straight forward: 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
    

提交回复
热议问题