・os モジュールを使う方法
>>> import os
>>> os.system(''grep "word" %s' % org_file')
結果を標準出力に出力することはできるが、返り値として取得はできない。
・command モジュールを使う方法
>>> import commands
>>> out_string = commands.getoutput('grep "word" %s' % org_file )
commands.getoutput の場合は標準出力を返り値として取得することができる。
Note) Python 3.0 では廃止。以下の subprocess を使うことを推奨されている。
・subprocess モジュールを使う方法
os.system, os.spawn*, os.popen*, popen2.*, commands.*
といった古いモジュールを置き換えるモジュールとして準備された。
詳細は http://docs.python.jp/2/library/subprocess.html.
>>> import subprocess
>>> out_string = subprocess.check_output(['grep', 'word', org_file])
で標準出力を取得できる。
例えば、org_file に "word" が含まれていなかったときなど、例外が発生するので、
try:
out_string = subprocess.check_output(['grep', 'word', org_file])
except:
#例外処理
とするのが良さそうだ。
>>> import os
>>> os.system(''grep "word" %s' % org_file')
結果を標準出力に出力することはできるが、返り値として取得はできない。
・command モジュールを使う方法
>>> import commands
>>> out_string = commands.getoutput('grep "word" %s' % org_file )
commands.getoutput の場合は標準出力を返り値として取得することができる。
Note) Python 3.0 では廃止。以下の subprocess を使うことを推奨されている。
・subprocess モジュールを使う方法
os.system, os.spawn*, os.popen*, popen2.*, commands.*
といった古いモジュールを置き換えるモジュールとして準備された。
詳細は http://docs.python.jp/2/library/subprocess.html.
>>> import subprocess
>>> out_string = subprocess.check_output(['grep', 'word', org_file])
で標準出力を取得できる。
例えば、org_file に "word" が含まれていなかったときなど、例外が発生するので、
try:
out_string = subprocess.check_output(['grep', 'word', org_file])
except:
#例外処理
とするのが良さそうだ。