简体   繁体   English

ValueError:错误的文件描述符

[英]ValueError: bad file descriptor

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))

查看此页面,您可以找到多个错误处理示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM