Python Forum
FileNotFoundError when I try putting sprite in Pygame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
FileNotFoundError when I try putting sprite in Pygame
#1
I started following a Space Invaders tutorial for pygame, and I made sprites in Piskel and put them into the folder with my game, but I got an error saying "FileNotFoundError: No such file or directory." Note that I am very new to pygame. I tried many ways, but it still comes up with the same error, which are listed below.

- pygame.image.load("cannon.png")
- pygame.image.load("cannon.gif")
- pygame.image.load("\Users\ZULU\AppData\Local\Programs\Python\Python37-32\Zion_Python Games\First Pygame\cannon.png")
- pygame.image.load("/Users/ZULU\AppData/Local/Programs/Python/Python37-32/Zion_Python Games/First Pygame/cannon.png")
- pygame.image.load(os.path.abspath("cannon.png"))
- pygame.image.load(os.path.abspath("\Users\ZULU\AppData\Local\Programs\Python\Python37-32\Zion_Python Games\First Pygame\cannon.png"))

Also, I tried checking if the path existed, but It always appeared False.

os.path.exists('/Users/ZULU/AppData/Local/Programs/Python/Python37-32/Zion_Python Games/First Pygame/cannon.png')

I'm just not sure what to do.
Reply
#2
Loading a file is in relation to the file that you ran to run the code.

I am assuming it is this based on your path?
pygame.image.load("First Pygame\cannon.png")
But that is assuming that your main file you run is in the directory "First Pygame" directory

If that fails i would give the fullpath. This is not the fullpath if you are on Windows. I am also assuming that based on seeing AppData directory.
Quote:
 pygame.image.load("\Users\ZULU\AppData\Local\Programs\Python\Python37-32\Zion_Python Games\First Pygame\cannon.png")

It should be
 pygame.image.load("C:\Users\ZULU\AppData\Local\Programs\Python\Python37-32\Zion_Python Games\First Pygame\cannon.png")
Recommended Tutorials:
Reply
#3
I tried adding the full path, but my code wouldn't run because of an error.
Error:
(unicode error) 'unicodescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
I'm not sure what this means.
Reply
#4
When use directories, it's "/" not "\".
Reply
#5
I just tried that, and I got the FileNotFound error.
Error:
Traceback (most recent call last): File "C:\Users\ZULU\AppData\Local\Programs\Python\Python37-32\Zion_Python Games\First Pygame\First Pygame.py", line 15, in <module> icon = pygame.image.load("C:/Users/ZULU/AppData/Local/Programs/Python/Python37-32/Zion_Python Games/First Pygame/cannon.png") FileNotFoundError: No such file or directory.
checking the path did appear false too when I checked it with the os module too, if that tells more of what is going on.
os.path.exists('C:/Users/ZULU/AppData/Local/Programs/Python/Python37-32/Zion_Python Games/First Pygame/cannon.png')
Any other suggestions?
Reply
#6
Try to put your image and source code in the same directory, and then use pygame.image.load("cannon.png") to see if it works.
Reply
#7
I've tried that, and I get the same error.
Reply
#8
Then I think you either don't have the image in the same directory, or got the image name wrong.
Reply
#9
It could be the directory name causing an issue
First Pygame
1) Is there a space here?
2) are you running on windows OS? I wasnt sure at first because you did not have the C drive in the path. The only reason i suspected windows was because i saw the AppData directory.

In windows OS you have to put quotation marks around the path if there is a space in a directory name such as:
"C:/Users/ZULU/AppData/Local/Programs/Python/Python37-32/Zion_Python Games/First Pygame/cannon.png"
However in linux and mac you escape it such as:
C:/Users/ZULU/AppData/Local/Programs/Python/Python37-32/Zion_Python Games/First\ Pygame/cannon.png
I havent used windows in a long time so i could be wrong. But i would try escaping the space in the path when you load it from pygame
pygame.image.load("C:/Users/ZULU/AppData/Local/Programs/Python/Python37-32/Zion_Python Games/First\ Pygame/cannon.png")
Recommended Tutorials:
Reply
#10
I figured it out!!

I put the file type in the name of the file, so the file name looked like "cannon.png" instead of "cannon," so i had to put an extra '.png' at the end.

pygame.image.load("C:/Users/ZULU/AppData/Local/Programs/Python/Python37-32/Zion_Python Games/First Pygame/cannon.png.png")
I feel so dumb. Thanks for the help though.
metulburr likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Sprites just randomly appear and dissapear in my pygame.sprite.GoupeSingle trueShadoWrr 2 1,980 Feb-13-2023, 09:34 AM
Last Post: Vadanane
  [PyGame] Pygame is treating blob_group as a surface, when I need it to treat it as a Sprite. Swagford 1 1,302 Jan-24-2023, 09:58 PM
Last Post: metulburr
  [PyGame] My Pygame Sprite not appearing... noodlespinbot 3 3,834 Oct-30-2020, 06:51 AM
Last Post: robinmurphy
  My Pygame Sprite not appearing... noodlespinbot 1 2,264 Apr-08-2020, 11:25 AM
Last Post: pyzyx3qwerty
  [PyGame] Sprite image.get_rect() moves sprite to 0, 0 michael1789 2 4,601 Dec-13-2019, 08:37 PM
Last Post: michael1789
  Pygame sprite not moving michael1789 1 2,832 Nov-10-2019, 03:54 AM
Last Post: michael1789
  Sprite not rendering Clunk_Head 2 2,140 Oct-03-2019, 11:27 AM
Last Post: Clunk_Head
  Need help making a sprite GalaxyCoyote 4 3,238 Aug-11-2019, 09:12 PM
Last Post: metulburr
  moving a sprite pfaber11 3 2,587 May-15-2019, 12:52 PM
Last Post: pfaber11
  Pygame Class Sprite Placement Confusion TheHumbleIdiot 2 3,556 Sep-11-2018, 02:19 PM
Last Post: TheHumbleIdiot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020