1
# load all images for the players
    animation_types = ['Idle', 'Run', 'Jump']
    for animation in animation_types:
        # reset temporary list of images
        temp_list = []
        # count number of files in the folder
        num_of_frames = len(os.listdir(f'Assets/{self.char_type}/{animation}'))
        for i in range(num_of_frames):
            img = pygame.image.load(f'Assets/{self.char_type}/{animation}/{i}.png')
            img = pygame.transform.scale(img, (int(img.get_width() * scale), int(img.get_height() * scale)))
            temp_list.append(img)
        self.animation_list.append(temp_list)

I am fairly new to python and pygame and am making my first game. I ran into a problem where i'm trying to access files for an animation. For the idle animation the directory is Assets/player/Idle. Inside the folder are 5 images. When I run the code I receive this error:

img = pygame.image.load(f'Assets/{self.char_type}/{animation}/{i}.png')
FileNotFoundError: No such file or directory

I am pretty sure the problem is with the images in the animation folder as I made sure the other folders were found and they were fine. I honestly don't know what to do. If you need the full script then I can send it. Thank you.

NKSM
  • 5,422
  • 4
  • 25
  • 38
  • Try deleting and reinstalling the pygame module – MundaneHassan Jul 27 '21 at 12:47
  • 3
    Use absolute paths, do not rely on relative paths, your CWD might not be what you expect it to be. Did you try to print out the formatted string? Did you check that there's actually a file at that location? – Mike Scotty Jul 27 '21 at 12:48
  • The CWD is in the right place, python can find all of the folders, the problem is that when it tries to load all the images it gives me the error. – James Thompstone Jul 27 '21 at 12:53
  • Does your animation folder contain files other than the animation PNGs? If so you'll try to load images that don't exist. You could try to explicitly iterate though only files that exist by using a [glob](https://docs.python.org/3/library/glob.html?highlight=glob#glob.glob) /[pathlib.glob](https://docs.python.org/3/library/pathlib.html#pathlib.Path.glob). Or you can explicitly check for file existence before loading, see [this venerable question](https://stackoverflow.com/q/82831/2280890) – import random Jul 27 '21 at 15:07
  • *"python can find all of the folders"* - How do you know that? The CWD is not the directory of the python source files. – Rabbid76 Jul 27 '21 at 15:20
  • Are you sure about `f'Assets/{self.char_type}/{animation}/{i}.png'`. Shouldn't it be `f'Assets/{self.char_type}/{animation}{i}.png'`? – Rabbid76 Jul 27 '21 at 15:21
  • All the animations are in different folders such as Idle, Run, Jump, Double Jump and the animations are inside named from 0 to how many frames there are in the animation - 1 for the index. – James Thompstone Jul 28 '21 at 08:19
  • The files do exist as I tested them individually and they are all there, they are all .png files and nothing else is in the folders for my animations. – James Thompstone Jul 28 '21 at 08:22
  • import os print(os.getcwd()) - I just ran this code and got the result "/Users/james/PycharmProjects/IDK". (IDK is the name of the project and all my folders are inside it) – James Thompstone Jul 28 '21 at 08:26

1 Answers1

1

I am on a linux system and I had a similar problem. This is what worked for me:

import os

PATH = os.path.dirname(os.path.abspath(__file__))

def get_path(*slices):
    return os.path.join(PATH, *slices)

First get the path to the containing directory then in the function return the joined path.
In your case you then need to call:

num_of_frames = len(os.listdir(get_path('Assets', self.char_type, animation))

and

img = pygame.image.load(get_paht('Assets', self.char_type, animation, str(i) + '.png')

other sources of the error:

  • you are on a windows machine (the path-splitting character on windows is not /)
  • the files aren't named correctly (0.png, 1.png, 2.png, 3.png, 4.png)

EDIT: Debugging idea: add

print('loading path =', f'Assets/{self.char_type}/{animation}/{i}.png')

before loading the image and check if this file actually exists.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Kesslwovv
  • 639
  • 4
  • 17
  • I tried this and got the error: "img = pygame.image.load(get_path('Assets', self.char_type, animation, str(i) + '.png'))" FileNotFoundError: No such file or directory. – James Thompstone Jul 28 '21 at 12:06
  • Also I used your debugging idea and got the output: loading path = Assets/player/Idle/0.png loading path = Assets/player/Idle/1.png loading path = Assets/player/Idle/2.png loading path = Assets/player/Idle/3.png loading path = Assets/player/Idle/4.png – James Thompstone Jul 28 '21 at 12:07
  • @JamesThompstone Did it crash after the `Assets/player/Idle/4.png`? if yes this means that it for some reason can't find this file, which means that this file is missing (make sure to check) or it doesn't have that exact name. make sure that there are no other files in 'Assets/player/Idle/' folder other than the 5 images. If the print statement is located after the loading it is possible that the error lies in 'Assets/player/Run/'. another idea is to try to load this specific file somewhere at the top of the program and see if it raises and Error as well. – Kesslwovv Jul 28 '21 at 12:37
  • Hey @Kesslwovv it can't find the file because it does not exist, for some reason when I am counting the number of files in the folder it counts 5 files rather than 4, it should be displaying the files 0.png, 1.png, 2.png, 3.png but it adds 4.png, I believe the problem might be in the num_of_frames variable. – James Thompstone Jul 29 '21 at 10:51
  • @JamesThompstone In your Question you say there are 5 images in the folder but in the comment you only mention 4. `os.listdir(dir)` lists all files and folders in the given `dir` folder. maybe there is an additional file or folder somewhere? – Kesslwovv Jul 29 '21 at 15:32
  • Sorry that's my bad, I thought there were 5 before but there are actually 4. I did num_of_frames = num_of_frames - 1 beneath my original variable and my code is working now. – James Thompstone Jul 29 '21 at 17:21