I'm using eSpeak on Ubuntu and have a Python 2.7 script that prints and speaks a message:
import subprocess
text = 'Hello World.'
print text
subprocess.call(['espeak', text])
eSpeak produces the desired sounds, but clutters the shell with some errors (ALSA lib..., no socket connect) so i cannot easily read what was printed earlier. Exit code is 0.
Unfortunately there is no documented option to turn off its verbosity, so I'm looking for a way to only visually silence it and keep the open shell clean for further interaction.
How can I do this?
if you happen to be using the subprocess module in windows (not specific to this question but matches title) with python 2.7x and it is ONLY the errors you want to suppress (specific to this question), you can do something like this:
output = subprocess.check_output(["arp", "-a", "-N", "127.0.0.2"], stderr=subprocess.STDOUT)
you should be able to test using the code above on your system but if 127.0.0.2 happens to exist in your arp table you can just pick an ip that does not have a nic associated with it.
Redirect the output to DEVNULL:
It is effectively the same as running this shell command:
As of Python3 you no longer need to open devnull and can call subprocess.DEVNULL.
Your code would be updated as such:
Here's a more portable version (just for fun, it is not necessary in your case):
Use
subprocess.check_output
(new in python 2.7). It will suppress stdout and raise an exception if the command fails. (It actually returns the contents of stdout, so you can use that later in your program if you want.) Example:You can also suppress stderr with:
For earlier than 2.7, use
Here, you can suppress stderr with
Why not use commands.getoutput() instead?