我一直在尝试为我的兄弟制作一个程序。其中一个组件是播放音频文件。我有一个大约 90 个音频文件的列表(请不要问我为什么有 90 个),我正在尝试随机选择一个并播放它。但是,要播放它,我必须找到它的路径,然后将路径插入我的代码的另一部分(我仍在修复中)。这是我到目前为止所拥有的:
import os, random
audio_playlist = [1, 2, 3, 4, ... all the way to 90]
sel_song = random.choice(audio_playlist)
song_path = None
base_directory = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"songs")
现在,这就是我如何创建随机选择的歌曲的路径:
while song_path == None:
if sel_song == 1:
song_path = os.path.join(directory, "1.mp3")
elif sel_song == 2:
song_path = os.path.join(directory, "2.mp3")
# and i do this 90 times... :(
有没有更蟒蛇的方式来做到这一点?另外,我该如何做才能设置歌曲的路径,这样我就不必编写数百行代码,而是使用非常简单的东西,只有大约 10-15 行代码。另请注意,为简单起见,其中的文件song_path
基本上只是.mp3
带有它的数字。