anechkayf
anechkayf

Reputation: 545

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:

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:

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!

Upvotes: 9

Views: 33481

Answers (2)

tekumara
tekumara

Reputation: 8807

This generally means something is capturing stdout (eg: pytest). In which case shutil.get_terminal_size().columns will return 80.

One option is to explicitly set columns via the environment variable COLUMNS of your process.

Upvotes: 2

dileepa
dileepa

Reputation: 53

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. Running the code in OS's native Terminal solved this problem for me.

Upvotes: 2

Related Questions