Pygame not recognizing my path but VS Code does?

81 views Asked by At

MY PYTHON VERSION IS 3.10

MY PYGAME VERSION IS 2.1.3

I have the following problem:

  • I'm using pygame, more especifically mixer. When defining the path of the archive I want to play, I run into the following error:

FileNotFoundError: No file '../Assets/Music/ability.mp3' found in working directory 'C:\Users\acmem\Desktop\programacion'.

  • I understand that there has to be some error with the path, of course, but when I'm tyoing the path in the file where I'm using mixer, VS Code recognizes the folder structure and even offers to auto-complete the path I'm writing (if I type '../', then VSCODE offers me to autocomplete with the folders in that level of the folder structure). For some reason I do not yet understand, python still tells me it cannot find the file but VS Code is.

The code of the mixer file is the following:

import pygame; from pygame import mixer; pygame.init(); import colorama; from colorama import Fore; colorama.init()

def new_ability():


     path = "../Assets/Music/ability.mp3"

     pygame.mixer.init()
     pygame.mixer.Sound(path).play()

The folder structure is the following:

JuegoTXT: Modules\abilities.py (the mixer file)
          
          Assets\Music\ability.mp3 (The file I want to play)
    

Any help is appreciated, thank you.

1

There are 1 answers

0
ykhrustalev On BEST ANSWER

Relative path depends on your working folder and could be different when you execute the script, try changing the code, so it is bound to the actual python file

...
import pathlib


root = pathlib.Path(__file__).parent
path = root / "../Assets/Music/ability.mp3"