0

我在尝试运行此代码以获取用户不和谐数据时遇到 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

注意:这仅在我运行 create 命令时发生。

我猜这是因为 discords 的 api 试图永远打开 JSON 文件。但我真的不知道如何解决它。

4

1 回答 1

1

你调用了你的函数open并且你在它内部调用它,这称为递归,只需给它另一个名字,一切都应该工作。

于 2021-07-07T13:51:08.810 回答