我在使用mpg321播放随机声音时遇到一些问题。 首先,我列出所有声音的列表,并将长度存储在一个变量中,然后创建一个介于0和该列表长度之间的随机数。我的问题是我不知道如何将它添加到字符串中操作系统()表示文件路径。在
sounds = os.listdir('./sounds/') # creates list of all sound names
totalSounds = len(sounds)
sound_number = random.randint(0, len(sounds))
next_sound = str(sounds[sound_number])
soundPlaying = True
os.system('mpg321 ./sounds/%s') % next_sound
soundPlaying = False
我尝试过使用%s并将变量放在./sounds/之后,但是我得到一个语法错误操作系统()只接受一个参数。在
感谢任何帮助。在
问题是,您需要对字符串而不是函数调用执行字符串格式设置
顺便说一下,我将使用subprocess,它提供了一个比操作系统! (https://docs.python.org/3/library/subprocess.html#subprocess.call)
^{pr2}$相关问题 更多 >
编程相关推荐