pythonraspberry-pisubprocessos.systemespeak

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


I want my raspberry pi to speak this print command

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

which has three variables in it. And this is the command i use in shell that i need to use in python

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

I've tried it with os.system because i'm not familiar with subprocess commands.

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

I'm getting an invalid sytax error. I've tried every version of it and couldn't make it work.


Solution

  • This should give you what you're looking for:

    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)