我希望将所有.mp3文件路径收集到一个文件夹(mp3)中,并将它们分配给字典中的键,这样无论系统文件路径如何,我的程序都能工作(目前,文件路径是硬编码的,这意味着该程序只在我自己的PC上工作)
以下是原始设置:
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)
更新:
我根据this answer重构了代码(谢谢!)。 我已经尝试通过修改playsound模块(如here所述)来查找实际问题,结果如下:
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不喜欢我正在做的事情
你核对过这个答案了吗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?
它概述了如何对此类错误进行故障排除,因为这是一种高级错误,实际错误可能是找不到文件或python版本控制错误
#######
另外,如果我可以对您的代码进行重构:
这将允许您添加任意数量的键,而无需添加新的“if语句”
相关问题 更多 >
编程相关推荐