3,665 views
この記事は最終更新から 1820日 が経過しています。
subprocess.call を使用して外部プログラムを実行してみる。
call は、外部プログラムの終了を待って復帰する。
call は、外部プログラムの戻り値を返す。
>>> import subprocess as sp
>>>
>>> arg = ['gcc','--version']
>>>
>>> ret = sp.call(arg)
gcc (GCC) 4.9.0
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>>>
>>> ret
0
subprocess.Popen でも同じことができる。
>>> import subprocess as sp >>> >>> arg = ['gcc','--version'] >>> >>> proc = sp.Popen(arg, stdout=sp.PIPE) >>> proc.returncode 0 >>> stdoutData, stderrData = proc.communicate() >>> >>> stdoutData 'gcc (GCC) 4.9.0\nCopyright (C) 2014 Free Software Foundation, Inc.\nThis is free software; see the source for copying conditions. There is NO\nwarranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n' >>> >>> out = stdoutData.split('\n') >>> out[0] 'gcc (GCC) 4.9.0' >>> >>> out[1] 'Copyright (C) 2014 Free Software Foundation, Inc.' >>> >>> out[2] 'This is free software; see the source for copying conditions. There is NO' >>> >>> out[3] 'warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.'