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.
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
.
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.