4

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()
Community
  • 1
  • 1
yan
  • 631
  • 9
  • 30

2 Answers2

7

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?

Supratim
  • 65
  • 4
  • I am using Python 2.7 – yan Jul 18 '14 at 07:39
  • Correct me if I am wrong, even as I tried this simple example `process = subprocess.Popen(['echo', 'Hello World!'], stdout=subprocess.PIPE)` as you have mentioned, the differences is between a `None` print out. But still I am getting the result `Hello World!` printed, is that not considered an output? – yan Jul 18 '14 at 08:12
  • 3
    Without subprocess.PIPE you wont be able to take the output returned by the command in a variable. That's the difference. Somewhat like print "Hello World" vs return "Hello World" – Supratim Jul 18 '14 at 08:44
  • Ok thanks... I am still somewhat 'un-convinced'? or rather not being able to see the logic how it is exactly working... Will tried to see it in the "Hello World" vs return "Hello World" example – yan Jul 18 '14 at 09:25
  • Let me try one more time: Let's say in your python script you have to write the output of "ps aux" (shell command) into a file. The file write part has to be in python not in shell, so you would need the result of "ps aux" in a variable that you can perform file.write(var) with. To do this, you have to set STDOUT --> subprocess.PIPE – Supratim Jul 18 '14 at 09:38
  • Thanks for the info. Kinda get what you mean but I think I may need to find more examples to work with, to drill it into my brain – yan Jul 21 '14 at 09:04
  • @yan: the correct way to write the output of `ps aux` is to redirect the output to file directly **without** subprocess.PIPE e.g., `with open('output', 'wb', 0) as file: subprocess.check_call(['ps', 'aux'], stdout=file)` – jfs Aug 06 '14 at 16:06
3

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() e.g., sockets on Unix).

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670