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
Splitting Out the Output of Ps Using Python
Listing Available Devices in Python-Opencv
How to Make a Python Script Run Like a Service or Daemon in Linux
Get Total Physical Memory in Python
Why Is Printing to Stdout So Slow? Can It Be Sped Up
Python Multiprocessing: Permission Denied
Python Ftp Get the Most Recent File by Date
How to Install Pyodbc on Linux
What Do I Need to Read Microsoft Access Databases Using Python
Show Matplotlib Plots (And Other Gui) in Ubuntu (Wsl1 & Wsl2)
Find Size and Free Space of the Filesystem Containing a Given File
Calling a Python Script from Command Line Without Typing "Python" First
Cqlsh Connection Error: 'Ref() Does Not Take Keyword Arguments'
Trouble Installing Scipy in Virtualenv on a Amazon Ec2 Linux Micro Instance
Why Do People Write #!/Usr/Bin/Env Python on the First Line of a Python Script
Download Large File in Python With Requests
Tkinter Creating Buttons in For Loop Passing Command Arguments