Use subprocess.check_output (new in python 2.7). It will suppresscaptures stdout as the return value of the function, which both prevents it from being sent to standard out and raisemakes it availalbe for you to use programmatically. Like subprocess.check_call, this raises an exception if the command fails. (It actually returns the contents of stdout, so you can use that later in your program ifwhich is generally what you want from a control-flow perspective.) Example:
import subprocess try: output = subprocess.check_output(['espeak', text]) except subprocess.CalledProcessError: # DoHandle somethingfailed call You can also suppress stderr with:
output = subprocess.check_output(["espeak", text], stderr=subprocess.STDOUT) For earlier than 2.7, use
import os import subprocess with open(os.devnull, 'w') as FNULL: try: output = subprocess._check_call(['espeak', text], stdout=FNULL) except subprocess.CalledProcessError: # DoHandle somethingfailed call Here, you can suppress stderr with
output = subprocess._check_call(['espeak', text], stdout=FNULL, stderr=FNULL)