0

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.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61

1 Answers1

0

Have you checked this answer? Why do I have "'utf-8' codec can't decode byte 0xc4 in position 0: invalid continuation byte " problem and how do I solve it?

It gives an overview in how to troubleshoot such error, since it is kind of a high level error, and the actual error might be a file not found or a python versioning error.

#######

Also if I may do some refactoring of your code:

NotePaths = {
    "C" : "","D" : "","E" : "","F" : "","G" : "","A" : "","H" : ""
}

def file_startswith_key(file):
    """ Checking if file ends with wanted keys """
    for key in NotePaths:
        if file.startswith(key)
        return key, True
    return None, False

for root, dirs, files in os.walk("/MP3"):
    for file in files:
        key, bool_value = file_startswith_key(file)
        if file.endswith(".mp3") and bool_value:
            NotePaths[key].append(Str("(os.path.join(root, file))"))

This would allow you to add as many keys as you want without adding new "if statements".

purple_lolakos
  • 456
  • 5
  • 15
  • Thank you for linking to the question and refactoring my code :) I looked at the question you linked, but I'm still stuck.. I updated the original question and it'd be great if you could take a look! –  Feb 12 '21 at 15:20