0

I need to load loads of images that are in a file, of which is in the same file as the .py file, but I keep getting this error: pygame.error: Couldn't open C:\Users\USER\Desktop\tester\animals

Here is my code:

import os
firstpath = str(os.path.dirname(os.path.abspath(__file__)))

I need to be able to access this sub file wherever itself and my script is.

def load_images():
    animal_dict = {}
    path = (firstpath+"\animals")
    animal = os.listdir(path)
    for images in animal:
        if images.endswith(".png"):
            path = os.path.join(path)
            key = images[:-4]
            animal_dict[key] = pygame.image.load(path).convert()
    return country_dict

load_images()
I.Kaur
  • 39
  • 1
  • 4
  • The error you're getting is because you are not loading a path to a `.png` file (you're trying to load a folder). That should be simple enough to fix. – Remolten May 02 '17 at 20:39
  • How can I load all the `.png` files located in that folder? – I.Kaur May 02 '17 at 20:44
  • Iterate over each image, and load it. – Remolten May 02 '17 at 20:45
  • I have no idea how to do that so I'll have to load them individually – I.Kaur May 02 '17 at 20:53
  • Where's the .py file with the function above in relation to the image directory? If it's in the `C:\Users\USER\Desktop\tester` directory and your images are in the `animals` directory, you can just write `for image in os.listdir('animals'):` and if the images are in the same directory `for image in os.listdir('.'):`. – skrx May 03 '17 at 00:09
  • Okay so once they're loaded, how can I assign each image to a variable, so `dog = dog.png`, since `gamedisplay.blit("dog.png",(0,0))` isn't going to work – I.Kaur May 03 '17 at 10:59

0 Answers0