python - IOError:[Errno 25] 设备的 ioctl 不合适
问题描述
我有以下代码来查找 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
在我的 test_something.py 文件中,我测试了一些调用get_default_console_width()的函数,它给了我这个错误:
IOError: [Errno 25] Inappropriate ioctl for device
我知道有一些类似的帖子有同样的问题,但我没有找到任何对这种情况有帮助的东西。
任何帮助表示赞赏!谢谢!
解决方案
大多数 IDE(如PyCharm或Wing)都模拟了终端,因此运行包含的代码get_terminal_size()
将触发 [Errno 25] Inappropriate ioctl for device
因为操作系统无法在 IDE 中获取模拟终端的行和列。在操作系统的本机终端中运行代码为我解决了这个问题。
推荐阅读
- javascript - 将元素升级为自定义元素
- swift - 一个页面有多个 UITextField 键盘不能隐藏
- c# - 为什么 OracleDataAdapter 在 C# 中断开连接?
- python - 将每个元素的大小定义为python中多维数组形状中的第三维/参数
- javascript - 提交按钮未在 ASP.NET MVC Web 应用程序中触发
- c++ - 我可以在 MacOS X 10.8 VM 上编译 c++ 以在当前的 Mac 上工作还是需要使用 MacOS 10.12 VM?
- css - 位置绝对时如何获得响应行为
- pandas - 熊猫转换为日期时间,dateutilparser 错误
- python - 从python中的库导入时出错
- python - 使用 tf.keras.optimizers.SGD 时出现 TensorFlow 错误