I'd like to gather all .mp3 file paths in a folder (MP3) and assign them to keys in a dictionary, so my program works regardless of the system filepath (currently, filepaths are hardcoded, meaning the program only works on my own PC).
Here's the original setup:
NotePaths = {
"C" : "C:/.../MP3/C4.mp3",
"D" : "C:/.../MP3/D4.mp3",
"E" : "C:.../MP3/E4.mp3",
"F" : "C:/.../MP3/F4.mp3",
"G" : "C:/.../MP3/G4.mp3",
"A" : "C:/.../MP3/A4.mp3",
"H" : "C:/.../MP3/H4.mp3"
}
Here's what I tried to do to make the program work independent of the system folder structure, as long as there is a folder in the same folder where the program is located called MP3 (which includes the needed .mp3 files):
NotePaths = {
"C" : "",
"D" : "",
"E" : "",
"F" : "",
"G" : "",
"A" : "",
"H" : ""
}
for root, dirs, files in os.walk("/MP3"):
for file in files:
if file.endswith(".mp3") and file.startswith("C"):
NotePaths["C"].append(Str("(os.path.join(root, file))"))
if file.endswith(".mp3") and file.startswith("D"):
NotePaths["D"].append(Str("(os.path.join(root, file))"))
if file.endswith(".mp3") and file.startswith("E"):
NotePaths["E"].append(Str("(os.path.join(root, file))"))
if file.endswith(".mp3") and file.startswith("F"):
NotePaths["F"].append(Str("(os.path.join(root, file))"))
if file.endswith(".mp3") and file.startswith("G"):
NotePaths["G"].append(Str("(os.path.join(root, file))"))
if file.endswith(".mp3") and file.startswith("A"):
NotePaths["A"].append(Str("(os.path.join(root, file))"))
if file.endswith(".mp3") and file.startswith("H"):
NotePaths["H"].append(Str("(os.path.join(root, file))"))
Attempting to run the program (which includes a playsound command to play the aforementioned .mp3 files) opens the playsound module and throws the following error:
Traceback (most recent call last):
File "C:\...\Musical Quiz.py", line 67, in <module>
playsound(currentPath)
File "C:\...\AppData\Local\Programs\Python\Python38\lib\site-packages\playsound.py", line 35, in _playsoundWin
winCommand('open "' + sound + '" alias', alias)
File "C:\...\AppData\Local\Programs\Python\Python38\lib\site-packages\playsound.py", line 30, in winCommand
'\n ' + errorBuffer.value.decode())
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe4 in position 60: invalid continuation byte
The filepaths are passed to the playsound function like this:
currentKey, currentPath = random.choice(list(NotePaths.items()))
playsound(currentPath)
Update:
I refactored my code according to this answer (thanks!). I have tried finding the actual problem as described here by modifying the playsound module (as described here) with the following result:
Traceback (most recent call last):
File "C:\Users\...\Musical Quiz.py", line 67, in <module>
playsound(currentPath)
File "C:\Users\...\AppData\Local\Programs\Python\Python38\lib\site-packages\playsound.py", line 35, in _playsoundWin
winCommand('open "' + sound + '" alias', alias)
File "C:\Users\...\AppData\Local\Programs\Python\Python38\lib\site-packages\playsound.py", line 31, in winCommand
raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException:
Error 292 for command:
open "" alias playsound_0.8907395801041198
Der Befehl erfordert einen Alias-, Datei-, Treiber- oder Gertenamen.
##Attempted translation: the command requires an alias, file name, driver name or (I don't know what the last thing is)
So it seems like a) my code never worked from the beginning and failed to get the correct filepaths or b) playsound doesn't like what I'm doing.