0

我想收集一个文件夹(MP3)中的所有 .mp3 文件路径并将它们分配给字典中的键,因此我的程序无论系统文件路径如何都可以工作(目前,文件路径是硬编码的,这意味着程序只能在我自己的情况下工作个人电脑)。

这是原始设置:

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"
}

这是我试图使程序独立于系统文件夹结构工作的方法,只要在程序所在的同一文件夹中有一个名为 MP3 的文件夹(其中包括所需的 .mp3 文件):

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))"))

尝试运行程序(其中包括播放上述 .mp3 文件的 playsound 命令)会打开 playsound 模块并引发以下错误:

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

文件路径传递给 playsound 函数,如下所示:

currentKey, currentPath = random.choice(list(NotePaths.items()))
playsound(currentPath)

更新

我根据这个答案重构了我的代码(谢谢!)。我尝试通过修改 playsound 模块(如此处所述)来找到此处描述的实际问题,结果如下:

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)          

所以看起来a)我的代码从一开始就没有工作并且未能获得正确的文件路径或b)playsound不喜欢我正在做的事情。

4

1 回答 1

0

你检查过这个答案吗?为什么我有“'utf-8' codec can't decode byte 0xc4 in position 0: invalid continuation byte”问题,我该如何解决?

它概述了如何解决此类错误,因为它是一种高级错误,实际错误可能是找不到文件或 python 版本控制错误。

#######

另外,如果我可以对您的代码进行一些重构:

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))"))

这将允许您添加任意数量的键,而无需添加新的“if 语句”。

于 2021-02-11T12:54:31.817 回答