技術メモBlog

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])

文字コード判定

>>> import chardet
>>> w = 'テキスト'
>>> char_dic = chardet.detect(w)
>>> {'encoding': 'utf-8', 'confidence': 0.99}


http://chardet.feedparser.org/
char_dic.get('confidence')
char_dic.get('encoding')

File Descriptor の変更

/proc/sys/fs/file-max には、128213となっていた。

sysctl -w fs.file-max=257925 と実行する