在python中是否有一种方法以编程方式确定控制台的宽度?我指的是一行中不换行的字符数,而不是窗口的像素宽度。
Edit
寻找在Linux上工作的解决方案
在python中是否有一种方法以编程方式确定控制台的宽度?我指的是一行中不换行的字符数,而不是窗口的像素宽度。
Edit
寻找在Linux上工作的解决方案
当前回答
我四处寻找,找到了窗户的解决方案:
http://code.activestate.com/recipes/440694-determine-size-of-console-window-on-windows/
这里有一个Linux的解决方案。
下面是一个在linux、os x和windows/cygwin上都可以运行的版本:
""" getTerminalSize()
- get width and height of console
- works on linux,os x,windows,cygwin(windows)
"""
__all__=['getTerminalSize']
def getTerminalSize():
import platform
current_os = platform.system()
tuple_xy=None
if current_os == 'Windows':
tuple_xy = _getTerminalSize_windows()
if tuple_xy is None:
tuple_xy = _getTerminalSize_tput()
# needed for window's python in cygwin's xterm!
if current_os == 'Linux' or current_os == 'Darwin' or current_os.startswith('CYGWIN'):
tuple_xy = _getTerminalSize_linux()
if tuple_xy is None:
print "default"
tuple_xy = (80, 25) # default value
return tuple_xy
def _getTerminalSize_windows():
res=None
try:
from ctypes import windll, create_string_buffer
# stdin handle is -10
# stdout handle is -11
# stderr handle is -12
h = windll.kernel32.GetStdHandle(-12)
csbi = create_string_buffer(22)
res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
except:
return None
if res:
import struct
(bufx, bufy, curx, cury, wattr,
left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
sizex = right - left + 1
sizey = bottom - top + 1
return sizex, sizey
else:
return None
def _getTerminalSize_tput():
# get terminal width
# src: http://stackoverflow.com/questions/263890/how-do-i-find-the-width-height-of-a-terminal-window
try:
import subprocess
proc=subprocess.Popen(["tput", "cols"],stdin=subprocess.PIPE,stdout=subprocess.PIPE)
output=proc.communicate(input=None)
cols=int(output[0])
proc=subprocess.Popen(["tput", "lines"],stdin=subprocess.PIPE,stdout=subprocess.PIPE)
output=proc.communicate(input=None)
rows=int(output[0])
return (cols,rows)
except:
return None
def _getTerminalSize_linux():
def ioctl_GWINSZ(fd):
try:
import fcntl, termios, struct, os
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,'1234'))
except:
return None
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
try:
cr = (env['LINES'], env['COLUMNS'])
except:
return None
return int(cr[1]), int(cr[0])
if __name__ == "__main__":
sizex,sizey=getTerminalSize()
print 'width =',sizex,'height =',sizey
其他回答
从Python 3.3开始,它是直接的: 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
import os
rows, columns = os.popen('stty size', 'r').read().split()
使用“stty size”命令,根据python邮件列表中的线程,该命令在linux上相当通用。它打开“stty size”命令作为文件,从中“读取”,并使用一个简单的字符串分割来分隔坐标。
不像操作系统。environ["COLUMNS"]值(尽管使用bash作为我的标准shell,但我不能访问),数据也将是最新的,而我相信os。environ["COLUMNS"]值只在python解释器启动时有效(假设用户自那时起调整了窗口的大小)。
(参见@GringoSuave关于如何在python 3.3+上做到这一点的回答)
不确定为什么它在模块shutil中,但它在Python 3.3中出现在那里。看到的:
查询输出终端的大小
>>> import shutil
>>> shutil.get_terminal_size((80, 20)) # pass fallback
os.terminal_size(columns=87, lines=23) # returns a named-tuple
底层实现在os模块中。跨平台工作在Linux, Mac OS和Windows,可能是其他unix类。还有一个后端口,虽然已经无关紧要了。
看起来代码有点问题,Johannes:
getTerminalSize需要导入操作系统 什么是env?看起来像os.environ。
另外,为什么要在返回之前切换线路和cols ?如果TIOCGWINSZ和stty都说行,那么cols,我说就这样吧。这让我困惑了10分钟,直到我注意到这种不一致。
Sridhar,我在管道输出时没有得到这个错误。我很确定它在尝试中被正确地捕获了——除了。
帕斯卡,“HHHH”在我的机器上不起作用,但“hh”可以。我很难找到这个函数的文档。它看起来依赖于平台。
chochem,合并。
以下是我的看法:
def getTerminalSize():
"""
returns (lines:int, cols:int)
"""
import os, struct
def ioctl_GWINSZ(fd):
import fcntl, termios
return struct.unpack("hh", fcntl.ioctl(fd, termios.TIOCGWINSZ, "1234"))
# try stdin, stdout, stderr
for fd in (0, 1, 2):
try:
return ioctl_GWINSZ(fd)
except:
pass
# try os.ctermid()
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
try:
return ioctl_GWINSZ(fd)
finally:
os.close(fd)
except:
pass
# try `stty size`
try:
return tuple(int(x) for x in os.popen("stty size", "r").read().split())
except:
pass
# try environment variables
try:
return tuple(int(os.getenv(var)) for var in ("LINES", "COLUMNS"))
except:
pass
# i give up. return default.
return (25, 80)
@reannual的回答很好,但有一个问题:os。Popen现在已弃用。应该使用subprocess模块,所以这里有一个版本的@reannual的代码,它使用subprocess并直接回答了这个问题(通过直接将列宽度作为int值给出:
import subprocess
columns = int(subprocess.check_output(['stty', 'size']).split()[1])
在OS X 10.9上测试