python - discord.py 中的 JSON RecursionError
问题描述
我在尝试运行此代码以获取用户不和谐数据时遇到 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 文件。但我真的不知道如何解决它。
解决方案
你调用了你的函数open
并且你在它内部调用它,这称为递归,只需给它另一个名字,一切都应该工作。
推荐阅读
- reactjs - 打开另一个字段的弹出器时隐藏一个字段的弹出器
- java - 如何在编译时使用 ASM 编辑 java 代码(使用 Gradle)
- facebook - 为什么 Facebook 发送 API 中的有效负载 URL 是可选的
- core-data - 在 SwiftUI 视图中发布后台上下文 Core Data 更改而不阻塞 UI
- php - getenv(ENVIRONMENT VARIABLE) 一无所获
- python - 使用 numpy 使用 delta 函数来收缩索引
- git - 安装 libsecret 凭据帮助程序后出现 git push 错误:“credential--h”不是 git 命令
- python - 使用 smtplib 发送电子邮件时附件重复
- excel - 无法获取 Worksheet 类的 PivotTables 属性
- html - 使用引导程序 4.3.1 在视频周围环绕文本