How to get Linux console window width in Python

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

    @reannual's answer works well, but there's an issue with it: os.popen is now deprecated. The subprocess module should be used instead, so here's a version of @reannual's code that uses subprocess and directly answers the question (by giving the column width directly as an int:

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

    Tested on OS X 10.9

提交回复
热议问题