ValueError:错误的文件描述符,宽度=os.get_terminal_size()列

2024-05-13 20:06:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到这个错误:

Traceback (most recent call last):
  File "C:\Users\myname\OneDrive\Desktop\DxubleG's follwo bot\bot.py", line 10, in <module>
    width = os.get_terminal_size().columns
ValueError: bad file descriptor

代码的部分是:

from colorama import init, Fore, Style
from discord.ext import commands
import subprocess
import requests
import discord
import ctypes
import os

init(convert=True)
width = os.get_terminal_size().columns
ctypes.windll.kernel32.SetConsoleTitleW('dxuble.lol')
token = 'Nzc0ODIxNjc1OTQyMjgxMjQ4.X6dW6w.sDk8D4FRFJXy06tEJCRLMqy6pFc'
prefix = '.'

title = [
    r'',
    r'',
    r'',
    r'',
    r'',
    r'',
    r''
]

intents = discord.Intents().all()
bot = commands.Bot(command_prefix=prefix, case_insensitive=True, intents=intents)
bot.remove_command('help')

有人知道为什么吗


Tags: columnsfromimportsizegetprefixinitos
2条回答

几乎可以肯定的是,由于以下三个原因之一,您会出现此问题:

  1. 没有终端
  2. 使用某个IDE提供的“虚拟终端”,该IDE没有将sys.stdout连接到真正的文件描述符,并且无法对os.get_terminal_size进行修补
  3. 当查询stdout的终端大小时,可能在一个奇怪的系统上,即使它连接到一个终端,也不起作用

无论哪种方式,解决方案都是相同的:Followthe guidance from the docs(“shutil.get_terminal_size()是通常应该使用的高级函数,os.get_terminal_size是低级实现。”),和use ^{},总是提供结果,按顺序检查:

  1. COLUMNS/LINES的环境设置
  2. 如果未定义它们,它将尝试使用os.get_terminal_size()
  3. 如果失败(无论是系统不支持查询,还是未连接到终端),它都会提供一个回退结果,默认为(80, 24)(如果对您来说这不是一个好的默认值,您可以传递自己的tuple,例如cols, lines = shutil.get_terminal_size((128, 32))回退到一个更大的默认维度)

您正在终端中运行此脚本吗? 我试图运行你的代码,但在我的终端中失败了,它按预期工作。 enter image description here

相关问题 更多 >