Python:Process
lastmod 2016-01-24created 2015-03-015 edits
Notes on running processes.
os.system
The return value indicates whether the command ran successfully.
subprocess
If the goal is just execution, as with os.system, use subprocess.call as shown below.
To use the printed stdout output, use subprocess.check_output.
import subprocess
result = subprocess.check_output('./program' , shell=True)
if result == 'AAA' :
print "123"
elif result == 'BBB' :
print "456"
STDERR can be obtained as follows.
from subprocess import Popen, PIPE, STDOUT
cmd = 'ls /etc/fstab /etc/non-existent-file'
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
output = p.stdout.read()
print output