-1

I am just an average noob playing with pygame, and I was wondering what is wrong with my file directory and image loading code?

import pygame, os
file_path = 'c:/users/' + os.getlogin()
bg_img = pygame.image.load(file_path+'/Documents/PYGAME/Assets/GUI/background_image.jpg') 

It comes up with a syntax issue:

Traceback (most recent call last):
    File "C:\Users\SVEN\Documents\PYGAME\C crawler.py", line 15, in <module>
    tePlayerNorth = pygame.image.load('north.png')
    FileNotFoundError: No file 'north.png' found in working directory 'C:\Users\SVEN\Documents\PYGAME'.

The directory C:\Users\SVEN\Documents\PYGAME is where the base script is, and the following folders are where the image is located.

This is me trying to recreate my folder structure:

C:/Users/SVEN/Documents/PYGAME
    SCRIPT.PY
    /Assets
        /Entities
            /Player
                North.png
                South.png
                East.png
                West.png

This is just the important part of the file-loading script, the rest is just code for the rest of the game. I have tried using the default Python loading items using /Assets/Entities/Player/North.png, but it does not work. That is all I can think of. Thanks for your help. :) (If you need the full code, I am happy to give it to you, just ask in the comments)

Sven
  • 7
  • 5
  • 1
    Because it's looking in `PYGAME`. If you want the file "Assets/Entities/Player/North.png", then that's what you have to pass to `load`. (Note: no leading slash.) – Tim Roberts Aug 23 '23 at 21:57
  • Just read the error message: *"No file 'north.png' found in working directory 'C:\Users\SVEN\Documents\PYGAME'"*. The problem is obvious, according to your file structure "north.png" is in a different directory – Rabbid76 Aug 23 '23 at 21:59
  • I see that, but I was also asking why it is doing that. Why it thinks its in a different directory? – Sven Aug 23 '23 at 21:59
  • 'north.png' is in 'C:\Users\SVEN\Documents\PYGAME\Assets/Entities/Player', but not in 'C:\Users\SVEN\Documents\PYGAME'. "It" doesn't do anything, you're just doing it wrong. The file path must be relative to the working directory. – Rabbid76 Aug 23 '23 at 22:02
  • Ok, so I have to have it in `C:\Users\SVEN\Documents\PYGAME` folder. It will look unorganised and that is why I am trying to use folders to seperate all of the 1000 images I need. – Sven Aug 23 '23 at 22:06
  • You have to load the files form it's folder: e.g.: `pygame.image.load('Assets/Entities/Player/north.png')` – Rabbid76 Aug 23 '23 at 22:08
  • So like this, `tePlayerNorth = pygame.image.load('Assets/Entities/Player/north.png')`. I have tried this, it is still asking for images in the `PYGAME` folder – Sven Aug 23 '23 at 22:48
  • No. It can't be, unless there are multiple places in your program where you are trying to load the image. – Rabbid76 Aug 24 '23 at 05:16

0 Answers0