Inexpetatus
Inexpetatus

Reputation: 21

File not found in working directory pygame

I am attempting to make a simple pygame game, and I need to load in some images. When I run my code, or:

bg_image = pygame.image.load('assets/Fondotiff.tif(1).png').convert_alpha()

It gives me an error which says:

    Traceback (most recent call last):
        File "C:\Users\victo\Desktop\Assets - Pulga\mainpulga.py", line 12, in <module>
    bg_image = pygame.image.load('assets/Fondotiff.tif(1).png').convert_alpha()
FileNotFoundError: No file 'assets/Fondotiff.tif(1).png' found in working directory 'C:\Users\victo\Desktop\Assets - Pulga'.
    
    ***Repl Closed***

I have double checked if the image is inside the folder on my desktop and it is there, but for some reason it doesn't find it.

How can I fix this?

Upvotes: 1

Views: 3051

Answers (2)

BounceBeachBall
BounceBeachBall

Reputation: 47

Another way you can do it is simply by also adding the file name of the file the script is inside of e.g.

image = pygame.image.load('-file with script inside-/-file with image inside-/-name of image-

Also make sure that the format of the image is compatible with pygame.

Upvotes: 0

Rabbid76
Rabbid76

Reputation: 210968

The asset file path has to be relative to the current working directory. The working directory is possibly different from the directory of the python file. It is not enough to put the files in the same directory or sub directory. You also need to set the working directory.

e.g.:

import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))

Upvotes: 3

Related Questions