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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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
.
回答2:
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.