抑制subprocess.Popen的输出

cof*_*fee 18 python subprocess

如何从输出中停止subprocess.Popen的输出?如果有很多印刷,印刷有时会很慢.

Bre*_*wey 24

如果你想完全扔掉它:

import subprocess
import os
with open(os.devnull, 'w') as fp:
    cmd = subprocess.Popen(("[command]",), stdout=fp)
Run Code Online (Sandbox Code Playgroud)

如果您使用的是Python 2.5,则需要from __future__ import with_statement或者不使用with.


jfs*_*jfs 16

在Python 3.3+中你可以subprocess.DEVNULL用来抑制输出:

from subprocess import DEVNULL, STDOUT, check_call

check_call([cmd, arg1, arg2], stdout=DEVNULL, stderr=STDOUT)
Run Code Online (Sandbox Code Playgroud)

stderr=STDOUT如果你不想抑制stderr也删除.