I'm a beginner programmer right now and am using the pygame "Mixer" module to call sound files from the directory. When I move them to a separate "Audio" folder they cannot be called without more direction, how would I specify that the sound files are in a specific folder in the directory?
I want my folder setup to resemble this:
DirectoryFolder:
Audio:
shoot.wav
src.py
shoot_sound = pygame.mixer.Sound('shoot.wav')
This only loads the audio if its in the same parent folder as the main .py file.
You can use the join() function of the ‘os’ module to access files in other directories. Example:
MyGame
-code
-src.py
-audio
-shoot.wav
import os
#other code
path = os.path.join('..', 'audio', 'shoot.wav')
shoot_sound = pygame.mixer.Sound(path)
#other code
The join() function returns a string containing all the arguments concatenated together using forward slashes. Passing '..' as an argument means to go up a level in the directory. In the code, the os.path.join() returns the following string:
../audio/shoot.wav
This path tells the script to go up one folder from the ‘code’ folder, which means into the ‘MyGame’ folder. Then, the script goes into the ‘audio’ folder and accesses the sound file.