【问题标题】:IOError: [Errno 25] Inappropriate ioctl for deviceIOError:[Errno 25] 设备的 ioctl 不合适
【发布时间】:2020-08-10 18:09:49
【问题描述】:

我有以下代码可以在 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

我知道有一些类似的帖子有同样的问题,但我没有找到任何对这种情况有帮助的东西。

感谢任何帮助!谢谢!

【问题讨论】:

  • 听起来stdout 已被重定向到不是控制台窗口的东西。
  • 标题与 IOError 有什么关系?你能发布完整的回溯吗?
  • @tdelaney 我的错,我不小心放错了标题。现在改了。
  • 如果 stdout 不是终端(例如,重定向到文件或作为子进程及其管道运行),就会发生这种情况。有不同的终端类型,例如通过串行线连接的物理终端,可能不接受该 ioctl。但如果这是一台 linux 机器和它的默认终端模拟器,它应该可以工作。这里只是猜测。

标签: python python-3.x python-2.7


【解决方案1】:

PyCharmWing 等大多数 IDE 都模拟了终端,因此运行包含 get_terminal_size() 的代码将触发 [Errno 25] Inappropriate ioctl for device,因为操作系统无法获得 IDE 中模拟终端的行。 在操作系统的本机终端中运行代码为我解决了这个问题。

【讨论】:

    猜你喜欢
    • 2010-12-08
    • 1970-01-01
    • 2014-09-17
    • 2015-02-03
    • 2021-11-25
    • 2015-10-30
    • 1970-01-01
    • 2022-06-27
    • 2013-01-28
    相关资源
    最近更新 更多