Redirecting stdio from a command in os.system() in Python

Leif Andersen picture Leif Andersen · Jul 7, 2010 · Viewed 38.8k times · Source

Usually I can change stdout in Python by changing the value of sys.stdout. However, this only seems to affect print statements. So, is there any way I can suppress the output (to the console), of a program that is run via the os.system() command in Python?

Answer

ealdent picture ealdent · Jul 7, 2010

On a unix system, you can redirect stderr and stdout to /dev/null as part of the command itself.

os.system(cmd + "> /dev/null 2>&1")