Could not open resource file, pygame error: "FileNotFoundError: No such file or directory."
Asked Answered
J

1

3
Import pygame

pygame.init()

BG = pygame.image.load('_pycache_/test_bg.jpg')

def DrawGameWin():
    window.blit(BG,(0,0))

pygame.display.update()


DrawGameWin()
Jesus answered 1/10, 2019 at 1:47 Comment(6)
The error message sounds like the file can't be found on the hard disk. Please check your current working directory and also the path of the file you want to open!Fortunetelling
They are both on my desktop in a folder. I'm unsure of what to do next. Where should I move my images I want to upload.Jesus
Here you can see how to check your current work directory. Please check that you're assuming it correctly. Alternatively you could also provide the full path for now, but this will destroy portability, so just do this as a last option.Fortunetelling
maybe im not understanding. I ran the program showed the path. I put both in the same folder on my desktop. isn't that the same path? Shouldn't that resolve the issue?Jesus
The run path(in other words the current working directory) is important, not folder in which your file is located.Fortunetelling
See also What exactly is current working directory?Circumfuse
S
7

The resource (image, font, sound, etc.) 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. Alternatively, you can create an absolute file path.


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

import os

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

An alternative solution is to find the absolute path. If the file is in an subfolder of the python file (or even in the same folder), then you can get the directory of the file and join (os.path.join()) the relative filepath. e.g.:

import pygame
import os

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

# [...]

# join the filepath and the filename
filePath = os.path.join(sourceFileDir, 'test_bg.jpg')
# filePath = os.path.join(sourceFileDir, '_pycache_/test_bg.jpg')

surface = pygame.image.load(filePath)

The same can be achieved with the pathlib module. Change the working directory

import os, pathlib

os.chdir(pathlib.Path(__file__).resolve().parent)

or create an absolute filepath:

import pathlib

# [...]

filePath = pathlib.Path(__file__).resolve().parent / 'test_bg.jpg'
surface = pygame.image.load(filePath)
Showily answered 1/10, 2019 at 4:57 Comment(2)
It's not incorrect that os.getcwd() returns the current working directory; but you don't need to know the current working directory to resolve a relative file name, as that's exactly what the OS does for you with a relative file name.Circumfuse
@Circumfuse No, you don't need to know that. But you can use this information to debug. And all the neybeeys who don't believe that and keep claiming that this this answer is wrong can see their mistake.Showily

© 2022 - 2024 — McMap. All rights reserved.