Search code examples
functionpython-2.7espeak

Python only passing the first word of my string to subprocess


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")

Solution

  • 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)