2011-08-16 53 views

回答

20

如果你想完全把它扔掉:

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

10

在Python 3.3+,您可以使用subprocess.DEVNULL,抑制輸出:

from subprocess import DEVNULL, STDOUT, check_call 

check_call([cmd, arg1, arg2], stdout=DEVNULL, stderr=STDOUT) 

刪除stderr=STDOUT如果你不想抑制stderr也。