Python only passing the first word of my string to subprocess

489 Views Asked by At

So i'm a week into learning python, apologies if this is obvious, can anyone tell me why i only get the first word of my sentence passed to espeak when i call the following functions yet the print command below it prints the whole thing? if i replace +x in the subprocess call with the text i want it works fine, is there some change of formatting i'm missing like somehow making my variable a string?

def speech(text):
    import subprocess
    x = text
    subprocess.call('espeak '+x, shell=True)
    print x

def exit():
    speech("Goodbye Slacker")
1

There are 1 best solutions below

0
On BEST ANSWER

Avoid using shell=True (since it can be a security risk, and properly quoting the text can be a nuisance). Instead, use shell=False and pass the arguments in a list:

subprocess.call(['espeak', x], shell=False)