Discord.py Cogs错误FileNotFoundError:[Errno 2]没有这样的文件或目录:'./cogs'

时间:2020-05-18 23:59:53

标签: python macos discord.py discord.py-rewrite

我正在用齿轮制造一个不和谐的机器人,当我运行代码时出现此错误:

FileNotFoundError: [Errno 2] No such file or directory: './cogs'

代码是:

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

cogs文件夹与Main.py位于同一目录中 我正在运行MacOS,并且我无法使用绝对路径,因为我要将其上传到heroku。在此先感谢您,如果您需要更多信息,请随时评论!

1 个答案:

答案 0 :(得分:1)

问题几乎可以肯定,当前工作目录不在cogs目录之上。以下是我用于为机器人加载所有齿轮的代码:

from os import listdir
from os.path import realpath, split, join, splitext
for item in listdir(join(split(realpath(__file__))[0], "cogs")):
    client.load_extension("cogs." + splitext(item)[0])