1

I've been trying to organize my game so I decided to put all my images into a separate folder. Now I'm trying to load the images again and it's giving me this error:

pygame.error: Couldn't open ./resouces/mage.png

Here's my code:

char = pg.image.load(os.path.join('./resouces','mage.png')).convert()

By the way, I've defined pg as pygame using: pg = pygame, I don't know if this is a problem or not.

I've also tried doing pg.image.load(./resources/mage.png), which gave me the same error.

Dat_Dude
  • 35
  • 1
  • 7

1 Answers1

1

The image filepath has to be relative to the current working directory. The working directory is possibly different to the directory of the python file.

The name and path of the file can be get by __file__. The current working directory can be get by os.getcwd().

If the image is in the same folder as the python file, then you cab get the directory of the file and concatenate the image filename. e.g.:

import pygame
import os

# get the directory of this file
sourceFileDir = os.path.dirname(os.path.abspath(__file__))

# [...]

char = pg.image.load(os.path.join(sourceFileDir,'./resouces','mage.png')).convert()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • so this gave me ```Couldn't open /Volumes/Xbox/a game/resouces/mage.png``` could the problem be that i have it on an external hard drive –  Nov 10 '19 at 18:37