2011-08-16 3 views

risposta

20

Se si vuole buttare tutto via:

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

Se si utilizza Python 2.5, avrete bisogno from __future__ import with_statement, o semplicemente non usare with.

10

In Python 3.3+ è possibile utilizzare subprocess.DEVNULL, per eliminare l'output:

from subprocess import DEVNULL, STDOUT, check_call 

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

Rimuovere stderr=STDOUT se non si desidera eliminare stderr anche.