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!