I'm trying to write a script in which I need to get the terminal size in order to adjust the output text width appropriately. I use this lines:
import os
x = os.get_terminal_size().lines
which results in getting this error:
ValueError: bad file descriptor
What's wrong with the code?
Try this:
import sys
import os
try:
cols,rows = os.get_terminal_size(0)
except OSError:
cols,rows = os.get_terminal_size(1)
sys.stdout.write('cols:{} \n rows:{} \n'.format(cols,rows))
查看此页面,您可以找到多个错误处理示例
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.