Could Not Open Resource File, Pygame Error: "Filenotfounderror: No Such File or Directory."

Could not open resource file, pygame error: FileNotFoundError: No such file or directory.

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)

FileNotFoundError: No such file or directory in pygame

The image file path has to be relative to the current working directory. The working directory is possibly different to the directory of the python file.

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).

One solution is to change the working directory:

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

An alternative solution is to find the absolute path.
If the image is relative to the folder 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 image filename. 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
fondImgPath = os.path.join(sourceFileDir, 'testingfield.png')

fond = pygame.image.load(fondImgPath)

python_pygame - FileNotFoundError: No such file or directory

r must be a prefix, not the content of the string. It has to be r"string" instead of "r'string":

background = pygame.image.load(("r'C:/Users/king/Desktop/practica/pygame_basic/background.png"))

background = pygame.image.load(r"C:/Users/king/Desktop/practica/pygame_basic/background.png")

String with prefix 'r' or 'R' are called raw strings and treat backslashes as literal characters. See String and Bytes literals.

Could not open resource file, pygame error: FileNotFoundError: No such file or directory.

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)


Related Topics



Leave a reply



Submit