[英]IOError: [Errno 25] Inappropriate ioctl for device
I have the following code to find the width of the console in Linux, which works in both Python 2.7 and Python 3.X:我有以下代码可以在 Linux 中找到控制台的宽度,它适用于 Python 2.7 和 Python 3.X:
def get_default_console_width():
try:
from shutil import get_terminal_size
console_width, rows = shutil.get_terminal_size()
except Exception:
import termios, fcntl, struct, sys
s = struct.pack('hh', 0, 0)
x = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, s)
rows, console_width = struct.unpack("hh", x)
return console_width
In my test_something.py file I test some function that calls get_default_console_width() and it gives me this error:在我的 test_something.py 文件中,我测试了一些调用get_default_console_width()的 function ,它给了我这个错误:
IOError: [Errno 25] Inappropriate ioctl for device
I know there are some similar posts with the same issue, but I did not find anything that would help in this case.我知道有一些类似的帖子有同样的问题,但我没有找到任何对这种情况有帮助的东西。
Any help is appreciated!任何帮助表示赞赏! Thanks!
谢谢!
Most IDE's like PyCharm or Wing has simulated Terminals so running a code contain get_terminal_size()
will trigger [ Errno 25] Inappropriate ioctl for device
because OS has no way of getting rows and columns of a Simulated Terminal inside a IDE.大多数 IDE,如PyCharm或Wing都模拟了终端,因此运行包含
get_terminal_size()
的代码将触发 [ Errno 25] Inappropriate ioctl for device
因为操作系统无法在 IDE 中获取模拟终端的行和列。 Running the code in OS's native Terminal solved this problem for me.在操作系统的本机终端中运行代码为我解决了这个问题。
This generally means something is capturing stdout (eg: pytest).这通常意味着某些东西正在捕获标准输出(例如:pytest)。 In which case
shutil.get_terminal_size().columns
will return 80
.在这种情况下,
shutil.get_terminal_size().columns
将返回80
。
One option is to explicitly set columns via the environment variable COLUMNS
of your process.一种选择是通过进程的环境变量
COLUMNS
显式设置列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.