python - I'm having trouble accessing midi files in a different folder -


my code, vincible.py, located in

c:\users\nick\desktop\vincible 

and i'm trying access 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:     event in pygame.event.get():       if event.type == pygame.quit:           sys.exit()     pygame.mixer.sound('title.midi').play() 

upon running it, runs except bit , i'm struggling figure out how this. don't typically work accessing files different paths in python, appreciated.

edit: subtly requested, removed try/except code. here 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:     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 , indeed in

vincible\audio 

but it's still returning error. i'd imagine has use of

os.path.join 

but again, i'm new os.path module. appreciated.

as fredrik recommended, tried revising code 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:     event in pygame.event.get():         if event.type == pygame.quit:             sys.exit()     titlemusic.play(-1) 

note: took away of other .midi files make code smaller/more readable, after issue has been resolved they'll added in correct manner.

when ran, interpreter raises pygame.error "the file couldn't opened" on line 13 (where load "title.midi"). appreciated.

you need specify folder when open file if not in same folder python file. may suggest opening files "audio/file.midi" instead of "file.midi". should work fine out function suggested fredrik. if change files stored (ie. rename folder or move files) don't forget change in code.

edit: also, side note irrelevant problem out nicely on project also, try getting files dynamically folder rather hard coding every file. try:

import os #this handy module operating system functions  def getmusic(place): #where place name of folder in case "audio/". don't forget slash!     file in os.listdir(place): #os.listdir returns list of files in directory.          pygame.mixer.music.load(place+file) 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -