This question already has an answer here:
I am calling a java program from my Python script, and it is outputting a lot of useless information I don't want. I have tried addind stdout=None
to the Popen function:
subprocess.Popen(['java', '-jar', 'foo.jar'], stdout=None)
But it does the same. Any idea?
From the documentation:
You need to set
stdout
tosubprocess.PIPE
, then call.communicate()
and simply ignore the captured output.although I suspect that using
subprocess.call()
more than suffices for your needs:From the 3.3 documentation:
So:
This only exists in 3.3 and later. But the documentation says:
And
os.devnull
exists way back to 2.4 (beforesubprocess
existed). So, you can do the same thing manually:Note that if you're doing something more complicated that doesn't fit into a single line, you need to keep
devnull
open for the entire life of thePopen
object, not just its construction. (That is, put the whole thing inside thewith
statement.)The advantage of redirecting to
/dev/null
(POSIX) orNUL:
(Windows) is that you don't create an unnecessary pipe, and, more importantly, can't run into edge cases where the subprocess blocks on writing to that pipe.The disadvantage is that, in theory,
subprocess
may work on some platforms thatos.devnull
does not. If you only care about CPython on POSIX and Windows, PyPy, and Jython (which is most of you), this will never be a problem. For other cases, test before distributing your code.