问题描述
我在尝试运行此代码以获取用户不和谐数据时遇到 RecursionError:
@bot.command(name="create")
async def create(ctx):
users = await open()
# some other code
def open(name="users.json",t="r"):
with open(name,t) as f:
return json.load(f)
出现以下错误:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py",line 85,in wrapped
ret = await coro(*args,**kwargs)
File "main.py",line 112,in create
users = await open()
File "main.py",line 54,in open
with open(name,t) as f:
File "main.py",t) as f:
[PrevIoUs line repeated 1483 more times]
RecursionError: maximum recursion depth exceeded
注意:这只会在我运行 create 命令时发生。
我猜这是因为 discords 的 api 试图永远打开 JSON 文件。但我真的不知道如何解决它。
解决方法
你调用了你的函数 open
并且你在它内部调用它,这叫做递归,只需给它另一个名字,一切都会正常。