简体   繁体   中英

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?

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.

eg:

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

Have you tried to use 'assets\Fondotiff.tif(1).png' instead of 'assets/Fondotiff.tif(1).png'?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM