JSON discord.py 中的递归错误
JSON RecursionError in discord.py
我在尝试 运行 此代码获取用户不一致数据时收到 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", line 54, in open
with open(name, t) as f:
File "main.py", line 54, in open
with open(name, t) as f:
[Previous line repeated 1483 more times]
RecursionError: maximum recursion depth exceeded
注意:这发生在我 运行 创建命令时。
我猜这是因为 discords 的 api 试图永远打开 JSON 文件。但是我真的不知道怎么解决。
你调用了你的函数 open
并且你在函数内部调用它,这叫做递归,只需给它另一个名字,一切都会起作用。
我在尝试 运行 此代码获取用户不一致数据时收到 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", line 54, in open
with open(name, t) as f:
File "main.py", line 54, in open
with open(name, t) as f:
[Previous line repeated 1483 more times]
RecursionError: maximum recursion depth exceeded
注意:这发生在我 运行 创建命令时。
我猜这是因为 discords 的 api 试图永远打开 JSON 文件。但是我真的不知道怎么解决。
你调用了你的函数 open
并且你在函数内部调用它,这叫做递归,只需给它另一个名字,一切都会起作用。