Monday, April 09, 2007

PythonからSHコマンド実行とテンポラリファイル

>>> import commands
>>> import os
>>>
>>> program = commands.getoutput("which コマンド")
>>> program = program.strip()
>>> o,i = os.popen4(program)
>>> list = i.readlines()
>>> o.close();i.colse()


>>> import tempfile
>>> fn = tempfile.mkstemp()
>>> file_buf = open(fn[1], 'wb')
>>> file_buf.write("テキストテキストテキスト")
>>> 何かの処理。
>>> file_buf.close()
>>> os.remove(fn[1])

0 Comments:

Post a Comment

<< Home