1import subprocess
2process = subprocess.Popen(['echo', 'More output'],
3 stdout=subprocess.PIPE,
4 stderr=subprocess.PIPE)
5stdout, stderr = process.communicate()
6stdout, stderr
7
1import subprocess
2subprocess.call("command1")
3subprocess.call(["command1", "arg1", "arg2"])
1#!/usr/bin/python
2import subprocess, sys
3## command to run - tcp only ##
4cmd = "/usr/sbin/netstat -p tcp -f inet"
5## run it ##
6p = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE)
7## But do not wait till netstat finish, start displaying output immediately ##
8while True:
9 out = p.stderr.read(1)
10 if out == '' and p.poll() != None:
11 break
12 if out != '':
13 sys.stdout.write(out)
14 sys.stdout.flush()
1>>> subprocess.check_output(['ls', '-l'])
2b'total 0\n-rw-r--r-- 1 memyself staff 0 Mar 14 11:04 files\n'