如何获取控制台窗口宽度在python [英] How to get console window width in python

查看:2497
本文介绍了如何获取控制台窗口宽度在python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在Python中以编程方式确定控制台的宽度?

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 of the window.

编辑

寻找适用于Linux的解决方案

Looking for a solution that works on Linux

推荐答案

import os
rows, columns = os.popen('stty size', 'r').read().split()

使用'stty size'命令,根据 python邮件列表上的线程在linux上是相当普遍的。它打开stty size命令作为文件,读取它,并使用简单的字符串拆分来分隔坐标。

uses the 'stty size' command which according to a thread on the python mailing list is reasonably universal on linux. It opens the 'stty size' command as a file, 'reads' from it, and uses a simple string split to separate the coordinates.

与os.environ [ COLUMNS]值(尽管使用bash作为我的标准shell,我不能访问)数据也将是最新的,而我相信os.environ [COLUMNS]值只会有效python解释器启动的时间(假设用户从那时起调整了窗口大小)。

Unlike the os.environ["COLUMNS"] value (which I can't access in spite of using bash as my standard shell) the data will also be up-to-date whereas I believe the os.environ["COLUMNS"] value would only be valid for the time of the launch of the python interpreter (suppose the user resized the window since then).

这篇关于如何获取控制台窗口宽度在python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆