如何使用文件夹中文件夹中的齿轮

问题描述 投票:0回答:2

我目前正在开发一个 Discord 机器人,并试图从多个文件夹中获取齿轮(更好的顺序),但我无法让它工作,请帮助我,哈哈

目前我使用这个系统并且它有效:

for cogpath in os.listdir("cogs"):
        if cogpath.endswith(".py"):
            client.load_extension(f'cogs.{cogpath[:-3]}')

我尝试用第二个字段扩展它:

for cogpath in os.listdir("cogs/Folder1"):
        if cogpath.endswith(".py"):
            client.load_extension(f'cogs.{cogpath[:-3]}')

但我得到的只是这样:

Error: Extension 'cogs.MyCog' could not be loaded.

如果你能帮助我那就太好了。 :)

python discord bots
2个回答
0
投票

你忘记了

load_extension
在新路径

for cogpath in os.listdir("cogs/Folder1"):
    if cogpath.endswith(".py"):
        client.load_extension(f'cogs.Folder1.{cogpath[:-3]}')

希望有帮助


0
投票

我尝试了 Cog.folder 但它只指定了一个文件夹。我提供的代码片段也会加载 cog 和子文件夹。

async def load():
for root, _, files in os.walk('./cogs'):
    for filename in files:
        if filename.endswith('.py'):
            path = os.path.join(root, filename)[len("./cogs/"):][:-3].replace(os.path.sep, '.')
            await client.load_extension(f'cogs.{path}')
© www.soinside.com 2019 - 2024. All rights reserved.