Come si interrompe l'output dal sottoprocesso.Popen dall'emissione? La stampa può a volte essere lenta se ce n'è una grande quantità.Elimina l'output dal sottoprocesso.Popen
11
A
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.