Question

My code, Vincible.py, is located in

C:\Users\Nick\Desktop\Vincible

and I'm trying to access the midi files located in

C:\Users\Nick\Desktop\Vincible\Audio

I've put together:

#!C:\Python33\python.exe
import sys, pygame, os.path

print("Loading...")
pygame.mixer.pre_init(44100, -16, 2, 2048)
try:
    pygame.mixer.music.load(os.path.join('Audio', 'title.midi'))
    pygame.mixer.music.load(os.path.join('Audio', 'dun1.midi'))
    pygame.mixer.music.load(os.path.join('Audio', 'dun2.midi'))
    pygame.mixer.music.load(os.path.join('Audio', 'dun3.midi'))
    pygame.mixer.music.load(os.path.join('Audio', 'dun4.midi'))
    pygame.mixer.music.load(os.path.join('Audio', 'dun5.midi'))
    pygame.mixer.music.load(os.path.join('Audio', 'dun6.midi'))
    pygame.mixer.music.load(os.path.join('Audio', 'echostheme'))
    pygame.mixer.music.load(os.path.join('Audio', 'town1.midi'))
    pygame.mixer.music.load(os.path.join('Audio', 'town2.midi'))
    pygame.mixer.music.load(os.path.join('Audio', 'town3.midi'))
except:
    raise UserWarning("Could not load or play soundfiles in 'Audio' folder")  
pygame.init()
size = width, height = 580, 444
screen = pygame.display.set_mode(size)
print("Loaded!")

while 1:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
          sys.exit()
    pygame.mixer.Sound('title.midi').play()

Upon running it, it runs the except bit and I'm struggling to figure out how to successfully do this. I don't typically work with accessing files from different paths in Python, so your help is very much appreciated.

Edit: As subtly requested, I removed the try/except from my code. Here is the new code:

#!C:\Python33\python.exe
import sys, pygame, os.path

print("Loading...")
pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.init()

pygame.mixer.music.load(os.path.join('Audio', 'title.midi'))
pygame.mixer.music.load(os.path.join('Audio', 'dun1.midi'))
pygame.mixer.music.load(os.path.join('Audio', 'dun2.midi'))
pygame.mixer.music.load(os.path.join('Audio', 'dun3.midi'))
pygame.mixer.music.load(os.path.join('Audio', 'dun4.midi'))
pygame.mixer.music.load(os.path.join('Audio', 'dun5.midi'))
pygame.mixer.music.load(os.path.join('Audio', 'dun6.midi'))
pygame.mixer.music.load(os.path.join('Audio', 'echostheme'))
pygame.mixer.music.load(os.path.join('Audio', 'town1.midi'))
pygame.mixer.music.load(os.path.join('Audio', 'town2.midi'))
pygame.mixer.music.load(os.path.join('Audio', 'town3.midi'))

size = width, height = 580, 444
screen = pygame.display.set_mode(size)
print("Loaded!")

while 1:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
          sys.exit()
    pygame.mixer.Sound('title.midi').play()

this time putting out:

Traceback (most recent call last):
  File "C:\Users\Nick\Desktop\Vincible\Vincible.py", line 8, in <module>
    pygame.mixer.music.load(os.path.join('Audio', 'title.midi'))
pygame.error: Couldn't open 'Audio\title.midi'

The file isn't corrupt and it is indeed in

Vincible\Audio

but it's still returning this error. I'd imagine it has something to do with my use of

os.path.join

but then again, I'm new to the os.path module. Any help is very much appreciated.

As Fredrik recommended, I tried revising my code to this:

#!C:\Python33\python.exe
import sys, pygame, os.path

print("Loading...")
pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.init()

def load(midi_file):
    module_directory = os.path.dirname(__file__)
    filename = os.path.join(module_directory, 'Audio', midi_file)
    pygame.mixer.music.load(filename)

load("title.midi")

size = width, height = 580, 444
screen = pygame.display.set_mode(size)
print("Loaded!")

while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    titlemusic.play(-1)

Note: I took away all of the other .midi files to make the code smaller/more readable, after the issue has been resolved they'll be added in the correct manner.

When ran, the interpreter raises another pygame.error "The file couldn't be opened" on line 13 (where I load "title.midi"). Any help is appreciated.

Was it helpful?

Solution 2

You need to specify the folder when open the file if it is not in the same folder as your python file. May I suggest opening the files as "Audio/file.midi" instead of just the "file.midi". That should work fine with out the function that was suggested by Fredrik. If you change where the files are stored (ie. you rename the folder or move the files) then don't forget to change it in the code.

EDIT: Also, a side note that is irrelevant to your problem but will help you out nicely on your project also, try getting the files dynamically from the folder rather than hard coding every file. Try:

import os #this is a handy module with operating system functions

def getMusic(place): #where place is the name of the folder in this case "Audio/". Don't forget the slash!
    for file in os.listdir(place): #os.listdir returns a list of all the files in a directory. 
        pygame.mixer.music.load(place+file)

OTHER TIPS

Paths are relative to the current directory, which isn't necessarily the same thing as the directory your script or module is in. To check what it is, add:

print os.getcwd()

To compute paths relative to a module directory, you can do something like this:

module_directory = os.path.dirname(__file__)
filename = os.path.join(module_directory, 'Audio', 'town3.midi')

(__file__ gives you the module's filename, and os.path.dirname strips off the file part)

A simple helper function might be useful:

def load(midi_file):
    filename = os.path.join(module_directory, 'Audio', midi_file)
    pygame.mixer.music.load(filename)

load('town1.midi')
load('town2.midi')
load('town3.midi')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top