Popen subprocess.PIPE and its uses

I know that this question may have been asked a lot but I am still not really getting it. Reading from this related link, I can understand why there is a need to add stdout=subprocess.PIPE at the end of the sentence so that the output result can be used into the next Popen.

Tried looking online, but I garner little to no knowledge about it as the ones I found are in the form of a documentation.

But if I am not using the output, is it really necessary to put stdout=subprocess.PIPE at the end? I tried executing it with and without the use of it, and it is still giving me the expected results that I wanted.

Hence what are the big differences whether subprocess.PIPE is present or not?

process = subprocess.Popen(command, stdout=subprocess.PIPE)
process.communicate()

Without subprocess.PIPE the output of command would be printed on STDOUT and process.communicate() should return None. Which python version are you using?

Can you paste the output?


If you don't read/write to the corresponding pipe then you should not use subprocess.PIPE . It may stall the child process if the corresponding OS pipe buffer fills up.

Use subprocess.PIPE if you want to get the output of the child process (or pass input) as a string (variable) or just call subprocess.check_output() that does it for you internally.

Use subprocess.PIPE if you want to pass process.stdout as stdin to another process (to emulate a | b shell command).

If you want to ignore the output; you could redirect it to DEVNULL. You could redirect it to a file (or to anything with a valid .fileno() eg, sockets on Unix).

链接地址: http://www.djcxy.com/p/77100.html

上一篇: 如何将stdout和stderr重定向到文件

下一篇: Popen子进程.PIPE及其用途