pythonjsondiscord.pyfilesystemsrecursionerror

JSON RecursionError in discord.py


I'm getting a RecursionError while trying to run this code to get users discord data:

@bot.command(name="create")
async def create(ctx):
  users = await open()
  # some other code

Which calls this function:

def open(name="users.json", t="r"):
  with open(name, t) as f:
    return json.load(f)

Getting the following Error:

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

Note: This happens just when I run the create command.

I guess that this happens because discords' api is trying to open the JSON file forever. But I don't really know how to fix it.


Solution

  • You called your function open and you’re calling it inside of it, that’s called recursion, simply give it another name and everything should work.