简体   繁体   English

IOError:[Errno 25] 设备的 ioctl 不合适

[英]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,如PyCharmWing都模拟了终端,因此运行包含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.

相关问题 Pexpect和PyCharm - 适用于设备的ioctl - Pexpect and PyCharm - Inappropriate ioctl for device X 服务器“localhost:10.0”上的 matplot lib“致命 IO 错误 25(设备的 ioctl 不合适)” - matplot lib "fatal IO error 25 (Inappropriate ioctl for device) on X server "localhost:10.0" 詹金斯的pipenv shell返回问题termios.error:(25,'不合适的设备ioctl') - pipenv shell in Jenkins return the issue termios.error: (25, 'Inappropriate ioctl for device') 崇高的tset:标准错误:设备的ioctl不适当 - Sublime tset: standard error: Inappropriate ioctl for device opencv无法阻止流:设备的ioctl不合适 - opencv Unable to stop the stream: Inappropriate ioctl for device 在Python中运行ioctl会返回ENOTTY-设备不适合的ioctl - Running ioctl in Python returns ENOTTY - inappropriate ioctl for device OpenCV3错误:“无法停止流:设备的不适当的ioctl” - OpenCV3 error: “Unable to stop the stream: Inappropriate ioctl for device” 如何抑制“stty:'标准输入':设备的 ioctl 不合适”错误 - How to supress the “stty: 'standard input': Inappropriate ioctl for device” error IOError:[Errno无效的输出设备(无默认输出设备)] -9996 - IOError: [Errno Invalid output device (no default output device)] -9996 PyAudio IOError:[Errno输入设备无效(无默认输出设备)] -9996 - PyAudio IOError: [Errno Invalid input device (no default output device)] -9996
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM