Disable console output from subprocess.Popen in Python
fh = open("NUL","w")
subprocess.Popen("taskkill /PID " + str(p.pid), stdout = fh, stderr = fh)
fh.close()
import os
from subprocess import check_call, STDOUT
DEVNULL = open(os.devnull, 'wb')
try:
check_call(("taskkill", "/PID", str(p.pid)), stdout=DEVNULL, stderr=STDOUT)
finally:
DEVNULL.close()
I always pass in tuples to subprocess as it saves me worrying about escaping. check_call ensures (a) the subprocess has finished before the pipe closes, and (b) a failure in the called process is not ignored. Finally, os.devnull
is the standard, cross-platform way of saying NUL
in Python 2.4+.
Note that in Py3K, subprocess provides DEVNULL for you, so you can just write:
from subprocess import check_call, DEVNULL, STDOUT
check_call(("taskkill", "/PID", str(p.pid)), stdout=DEVNULL, stderr=STDOUT)