Getting a ‘No such file or directory error’, despite the computer getting the name of the file itself

  discord.py, python

I’m trying to get my bot to post random images, but it keeps saying it can’t find the file despite having gone into the directory earlier to append the name to a list. I’ve already tried a help discord, and tried running the script from command prompt instead of VSC’s terminal. Any ideas? The images are within the folder ‘Images’.This is here to show you how I set out the folders.

Exact error:

  File "C:Python38libsite-packagesdiscordextcommandsbot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "C:Python38libsite-packagesdiscordextcommandscore.py", line 859, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:Python38libsite-packagesdiscordextcommandscore.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: FileNotFoundError: [Errno 2] No such file or directory: 'Polish_20200821_210116778.jpg'

Code from the cog:

import random
import os
from discord.ext import commands

class BlursedImages(commands.Cog):
    def __init__(self, client):
        self.client = client
    #Events
    @commands.Cog.listener()
    async def on_ready(self):
        print('BlursedImages is online.')

    @commands.command(brief = "Carlos meme", description = "Posts a really low effort Carlos Sainz meme")
    async def carlosmeme(self, ctx):
        carlosmemes = []
        for filename in os.listdir('./Images'):
            if filename.endswith('.png') or filename.endswith('.jpg'):
                carlosmemes.append(filename)
        randomimage = random.choice(carlosmemes)
        await ctx.send(file=discord.File(randomimage))



def setup(client):
    client.add_cog(BlursedImages(client))

Source: Python Questions

LEAVE A COMMENT