1) The standard way of executing an application without capturing its output is to use the call function:
from subprocess import call
try:
returncode = call(cmd, shell=True)
if returncode:
print 'Failure with returncode', returncode; sys.exit(1)
except OSError, message:
print 'Execution failed! \n', message; sys.exit(1)
2) 使用Popen object:
例子如下 ----
from subprocess import Popen, PIPE
cmd = 'myprog -c file.1 -p <>
p = Popen(cmd, shell=True, stdout=PIPE)
output, errors = p.communicate()
另一個例子是在程式中使用 (ex. SPIDER這種interactive的program) ----
pipe = Popen('gnuplot -persist', shell=True, stdin=PIPE).stdin
pipe.write('set xrange [0:10]; set yrange [-2:2]\n')
pipe.write('plot sin(x)\n')
pipe.write(quit') # quit Gnuplot
沒有留言:
張貼留言