Python - error: couldn't open .png file

Use relative paths instead (it's always better to do so) :

import os

current_path = os.path.dirname(__file__) # Where your .py file is located
resource_path = os.path.join(current_path, 'resources') # The resource folder path
image_path = os.path.join(resource_path, 'images') # The image folder path

By doing this, wherever you move the folder containing your .py file, its subdirectories (and therefore whatever they contain) can still be accessed without you having to modify your code.


Final code :

import pygame
import os
from pygame.locals import *


pygame.init()

width, height = 640, 480
screen = pygame.display.set_mode((width, height))

current_path = os.path.dirname(__file__) # Where your .py file is located
resource_path = os.path.join(current_path, 'resources') # The resource folder path
image_path = os.path.join(resource_path, 'images') # The image folder path

player_image = pygame.image.load(os.path.join(image_path, 'dude.png'))

while 1:

    screen.fill(0)

    screen.blit(player, (100,100))

    pygame.display.flip()

    for event in pygame.event.get():


        if event.type==pygame.QUIT:
            pygame.quit()
            exit(0)

Use this accessing method for all your other files, and you'll avoid a bunch of problems.

Tags:

Python

Pygame