cxyro
cxyro

Reputation: 21

Error "list index out of range" in Python 3

I am currently programming a Discord Bot for a friend. I have a new PC and now the following error occured after using my "!meme" command. It worked on my old Laptop, but i dont have it anymore...

Code for the command:

elif '!meme' in message.content:
    random_message_i = ["Here you go:", "Classic one:", "This one is good:", "lol xD:"]
    await message.channel.send(f"{random.choice(random_message_i)}")
    meme_file_paths = [os.path.abspath(_) for _ in glob.glob("botmemes/*.png")]
    random_meme_i = random.choice(meme_file_paths)
    await message.channel.send(file=discord.File(random_meme_i))
    print("[INFO] Last command: !meme")

Error:

   Traceback (most recent call last):
      File "C:\Users\cxyro\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 409, in _run_event
        await coro(*args, **kwargs)
      File "c:\Users\cxyro\Desktop\DiscordBot\discordbot.py", line 103, in on_message
        random_meme_i = random.choice(meme_file_paths)
      File "C:\Users\cxyro\AppData\Local\Programs\Python\Python310\lib\random.py", line 378, in choice
        return seq[self._randbelow(len(seq))]
    IndexError: list index out of range

(Yes, the code ant the memes are in the same main folder...)

Can somebody help me?

Upvotes: 0

Views: 538

Answers (1)

PRINCE
PRINCE

Reputation: 56

As far as, I can see , it occurs when you pass an empty list to choice() function. (in case, file not found) P.S :- Make sure, you are executing that script within the same directory where your code and meme is kept. otherwise, your current working directory gets changed and your code won't be able to find the meme path. you can verify this by writing this code in a print statement in your script:

import os; os.getcwd()

Upvotes: 1

Related Questions