How do you stop the output from subprocess.Popen from being output? Printing can sometimes be slow if there is a great deal of it.
Asked
Active
Viewed 2.6k times
30
-
Almost duplicate of [python - How to hide output of subprocess - Stack Overflow](https://stackoverflow.com/questions/11269575/how-to-hide-output-of-subprocess) (Popen versus call) – user202729 Dec 05 '21 at 16:49
3 Answers
30
In Python 3.3+ you could use subprocess.DEVNULL
, to suppress the output:
from subprocess import DEVNULL, STDOUT, check_call
check_call([cmd, arg1, arg2], stdout=DEVNULL, stderr=STDOUT)
Remove stderr=STDOUT
if you don't want to suppress stderr
also.

jfs
- 399,953
- 195
- 994
- 1,670
-
This also worked for me (Python 3.6, Ubuntu Linux OS): subprocess.Popen(cmd, shell=False, stdout=DEVNULL) – Jamie Nicholl-Shelley May 07 '21 at 13:31
30
If you want to totally throw it away:
import subprocess
import os
with open(os.devnull, 'w') as fp:
cmd = subprocess.Popen(("[command]",), stdout=fp)
If you are using Python 2.5, you will need from __future__ import with_statement
, or just don't use with
.

Brent Newey
- 4,479
- 3
- 29
- 33
-
1This no longer works. Please update. TypeError: popen() got an unexpected keyword argument 'stdout' – James Dalgleish Jun 14 '22 at 21:35
0
This also worked for me (Python 3.6, Ubuntu Linux OS):
subprocess.Popen(cmd, shell=False, stdout=DEVNULL)
-This assumes you want non blocking call and no junk in the console from the cmd.

Jamie Nicholl-Shelley
- 526
- 1
- 7
- 21