需要在 os.system 中将变量与 python 中的 espeak 和 aplay 命令一起使用

Need to use a variable in os.system with espeak and aplay commands in python

我希望我的 raspberry pi 说出这个打印命令

print label + " is " + spos + " and the distance is " + str(distance) + "cm"

其中包含三个变量。 这是我在 shell 中使用的命令,我需要在 python

中使用
$espeak -s110  "label + " is " + spos + " and the distance is " + str(distance) + "cm"" --stdout | aplay -D sysdefault:CARD=2

我已经用 os.system 试过了,因为我不熟悉 subprocess 命令。

os.system('espeak -s110  "'label' + is + 'spos' +  and the distance is  + 'str(distance)' + cm" --stdout | aplay -D sysdefault:CARD=2')

我收到一个无效的语法错误。我已经尝试了它的每个版本,但无法正常工作。

这应该能满足您的需求:

cmd = 'espeak -s110 "{0} is {1} and the distance is {2} cm" --stdout | aplay -D sysdefault:CARD=2'.format(label, spos, str(distance))

os.system(cmd)