11
如何停止subprocess.Popen的輸出輸出?如果有大量印刷,印刷有時會變慢。從子流程中取消輸出.Popen
如何停止subprocess.Popen的輸出輸出?如果有大量印刷,印刷有時會變慢。從子流程中取消輸出.Popen
如果你想完全把它扔掉:
import subprocess
import os
with open(os.devnull, 'w') as fp:
cmd = subprocess.Popen(("[command]",), stdout=fp)
如果您正在使用Python 2.5,則需要from __future__ import with_statement
,或者根本就沒有使用with
。
在Python 3.3+,您可以使用subprocess.DEVNULL
,抑制輸出:
from subprocess import DEVNULL, STDOUT, check_call
check_call([cmd, arg1, arg2], stdout=DEVNULL, stderr=STDOUT)
刪除stderr=STDOUT
如果你不想抑制stderr
也。