Python Subprocess usage summary

import subprocess
cmd = ['command', 'option1', 'option2', '...']
Run a command, block until it finishes, and get the return code
returncode =
Run a command, block until it finishes, and get its output to stdout and stderr and the return code
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
returncode = proc.returncode
Run a command, do not block but read the output from the process, and get the return code when it finishes
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

import sys
output_text = ''
while True:
	output =
	if output == '': break
	output_text += output

returncode = proc.wait()
Run a command, and send the input to the process
input_text = 'some input to the process'
proc = subprocess.Popen(cmd, stdin=PIPE)
Python Subprocess usage summary