0

I am a programming beginner. I recently had a problem with Python when running pygame that said:

FileNotFoundError: No such file or directory.

I wanted to add an image that came out. This is my code here in case someone can help.

current_path = os.path.dirname(__file__)
resource_path = os.path.join(current_path, 'resources ') 
image_path = os.path.join(resource_path, 'images')

#Knight
knight = pygame.image.load(os.path.join(image_path, 'nicti.png'))
hrokr
  • 3,276
  • 3
  • 21
  • 39

2 Answers2

0

os.path.dirname(path) just the first element of the pair returned by passing path to the function split().
Use os.path.abspath() to return a normalized absolutized version of the path:

current_path = os.path.dirname(__file__)

current_path = os.path.dirname(os.path.abspath(__file__))
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
0

Alternatively, as long as your image is in the same folder as your program, you could just go:

knight = pygame.image.load('knight.png')

If it is in a subfolder, (I usually have a folder called textures for images):

knight = pygame.image.load(r'textures\knight.png')

So yours will be (as long as it is in the same folder as your program):

knight = pygame.image.load(r'resources\images\knight.png')

No fancy stuff

Wolfmann Games
  • 142
  • 1
  • 13