Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable console output from subprocess.Popen in Python

I run Python 2.5 on Windows, and somewhere in the code I have

subprocess.Popen("taskkill /PID " + str(p.pid))

to kill IE window by pid. The problem is that without setting up piping in Popen I still get output to console - SUCCESS: The process with PID 2068 has been terminated. I debugged it to CreateProcess in subprocess.py, but can't go from there.

Anyone knows how to disable this?

like image 682
Denis Masyukov Avatar asked Aug 07 '09 13:08

Denis Masyukov


People also ask

Does subprocess Popen block?

Popen is nonblocking. call and check_call are blocking. You can make the Popen instance block by calling its wait or communicate method.

How do I capture the output of a subprocess run?

To capture the output of the subprocess. run method, use an additional argument named “capture_output=True”. You can individually access stdout and stderr values by using “output. stdout” and “output.

What is the difference between subprocess run and Popen?

The main difference is that subprocess. run() executes a command and waits for it to finish, while with subprocess. Popen you can continue doing your stuff while the process finishes and then just repeatedly call Popen. communicate() yourself to pass and receive data to your process.


1 Answers

fh = open("NUL","w")
subprocess.Popen("taskkill /PID " + str(p.pid), stdout = fh, stderr = fh)
fh.close()
like image 147
Mark Avatar answered Sep 18 '22 10:09

Mark