Is there a quiet version of subprocess.call?

Is there a variant of subprocess.call that can run the command without printing to standard out, or a way to block out it's standard out messages?


Solution 1:

Yes. Redirect its stdout to /dev/null.

process = subprocess.call(["my", "command"], stdout=open(os.devnull, 'wb'))

Solution 2:

Often that kind of chatter is coming on stderr, so you may want to silence that too. Since Python 3.3, subprocess.call has this feature directly:

To suppress stdout or stderr, supply a value of DEVNULL.

Usage:

import subprocess
rc = subprocess.call(args, stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)

If you are still on Python 2:

import os, subprocess

with open(os.devnull, 'wb') as shutup:
    rc = subprocess.call(args, stdout=shutup, stderr=shutup)