首页 > 其他分享 >获取当前运行的终端大小

获取当前运行的终端大小

时间:2024-07-03 14:31:50浏览次数:1  
标签:get terminal 获取 终端 模块 shutil os 运行 size

方法

shutil 模块有一个方法 .get_terminal_size,通过这个方法可以获取到当前运行的终端大小。
比如:

>>> import shutil

>>> shutil.get_terminal_size()
os.terminal_size(columns=195, lines=52)

>>> shutil.get_terminal_size().columns
195

将终端窗口缩小再次运行,会得到对应的值:

>>> shutil.get_terminal_size()
os.terminal_size(columns=120, lines=30)

该方法接收一个默认参数,用来在获取失败时返回,默认值为 (80 , 24),如果有定义环境变量 COLUMNSLINES,且环境变量的值是一个正整数的话,那么会优先返回环境变量所定义的值。


扩展

os 模块也有一个同名的方法,不过根据其文档所说,更推荐使用 shutil 模块所提供的方法,因为 os 模块定义的方法是其更低层次的实现(shutil 模块的文档也说明,它是通过调用 os 模块所获取的)。
当然,实际使用过程中,两个模块所表现的几乎类似,不过 os 模块支持更多的底层的参数。


相关文档

shutil.get_terminal_size

Get the size of the terminal window.

For each of the two dimensions, the environment variable, COLUMNS
and LINES respectively, is checked. If the variable is defined and
the value is a positive integer, it is used.

When COLUMNS or LINES is not defined, which is the common case,
the terminal connected to sys.stdout is queried
by invoking os.get_terminal_size.

If the terminal size cannot be successfully queried, either because
the system doesn't support querying, or because we are not
connected to a terminal, the value given in fallback parameter
is used. Fallback defaults to (80, 24) which is the default
size used by many terminal emulators.

The value returned is a named tuple of type os.terminal_size.


os.get_terminal_size

Return the size of the terminal window as (columns, lines).

The optional argument fd (default standard output) specifies
which file descriptor should be queried.

If the file descriptor is not connected to a terminal, an OSError
is thrown.

This function will only be defined if an implementation is
available for this system.

shutil.get_terminal_size is the high-level function which should
normally be used, os.get_terminal_size is the low-level implementation.


参考

  1. shutil — High-level file operations — Python 3.12.4 documentation
  2. os — Miscellaneous operating system interfaces — Python 3.12.4 documentation

标签:get,terminal,获取,终端,模块,shutil,os,运行,size
From: https://www.cnblogs.com/kingron/p/18281567

相关文章

  • 使用qt 获取一张网络图片,并将这张图片显示在qml 上
    #include<QGuiApplication>#include<QQmlApplicationEngine>#include<QNetworkAccessManager>#include<QNetworkReply>#include<QUrl>#include<QImage>#include<QQuickImageProvider>classImageProvider:publicQQuickI......
  • 《安富莱嵌入式周报》第339期:单片机运行苹果早期Mac系统模拟器,2GHz示波器有源探头,下一
    周报汇总地址:http://www.armbbs.cn/forum.php?mod=forumdisplay&fid=12&filter=typeid&typeid=104 视频版https://www.bilibili.com/video/BV1Kf421Q7Lh目录1、开源2GHz的示波器有源探头2、模拟矩阵开关面包板Jumperless推出下一代JumperlessV53、软件相关(1)Swifton......
  • 课前准备---HD数据结合图像识别获取真实的空间单细胞级数据
    作者,EvilGeniusHD数据不同于Xenium,目前还是横屏竖直的一刀切数据分析模式,但是真实的细胞绝对不是如此分布的,那么实际分析中,2um的精度配合图像的信息,获取真实的细胞分布数据,就成了分析的必须。多说一句,分析的准确性和超前化也是公司对核心分析人员的核心要求。如下如,我们最......
  • 防爆对讲终端是什么?在哪些行业中应用广泛?
    防爆对讲终端是一种特殊设计的通信设备,它具备防爆性能和可靠的通信功能,确保在存在爆炸性气体或粉尘的危险环境中使用时不会引发爆炸或火灾等危险情况。这种设备通过特殊的设计和防护措施,如采用防爆材料、防静电、绝缘、阻燃材料等,以及特殊的防爆电路设计,来防止静电火花、电路短......
  • thinkphp6 使用FFMpeg获取视频信息
    1.本地安装 FFMpeg,官网下载地址:https://ffmpeg.org/download.html#build-windows  解压后,把文件夹放到自定义目录,添加系统变量 2.安装依赖,composer.json添加"php-ffmpeg/php-ffmpeg":"^0.19.0",3.封装class类<?phpnamespaceapp\api\controller;useFFMpeg......
  • 修复objectps.dll丢失问题:确保系统与软件稳定运行的实战策略
    objectps.dll是一个与特定软件或Windows操作系统中某些功能相关的动态链接库文件。它可能涉及到对象链接与嵌入(OLE)、打印服务或其他系统组件。当系统提示objectps.dll丢失时,意味着该文件可能已被意外删除、损坏或未正确注册,影响到依赖此DLL的应用程序运行。修复objectps.dl......
  • 搭建工程之一 eclipse 中基于 maven 的 webapp工程能基于tomcat运行
    一、背景  作为开发人员,开发的web(运行在tomcat容器中)希望能够在本地开发工具(eclipse)中运行调试,加快开发测试进度。二、操作步骤1、创建maven工程 在eclipse上右键,选择"New"---"Other"---"Maven"----"MavenProject" 勾选上“Createasimpleproject”......
  • 主键Id自增,如何获取Id(Dapper)
    这里用的是Dapper,以前用EF的时候好像有用到过db.savechanges().但是项目中没有这个,所以用以下的方法去获取id背景:涉及到多表入库,需要获取主表的Id,所以用到了这个(timeFields 可以忽略)  ///<summary>///单个添加///</summary>///<typ......
  • nginx配置获取客户端的真实ip
    https://blog.csdn.net/superzhang6666/article/details/132901093 对于nginx获取客户端真实ip做个总结对于首层代理服务器,使用proxy_set_headerX-Forwarded-For$remote_addr;来将客户端IP赋值给X-Forwarded-For请求头对于非首层代理服务器,使用proxy_set_headerX-Forwarded......
  • 【《视觉十四讲》例程运行记录】——运行ch9后端优化CeresBA和g2o求解BA的实践例程
    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录一、运行ch9的例程代码1.MeshLab安装2.编译例程代码前的修改3.编译例程二可能的报错:c++:internalcompilererror:已杀死(programcclplus)1.问题描述2.原因分析3.解决总结一、运行ch9......