看板 Python 關於我們 聯絡資訊
要在Python執行外部程式, 有下列幾種方法 1. 在shell執行指令 os.system("ls --all --almost-all") 2. 不另開shell subprocess.call(['ls', '--all','-almost-all']) 我在GitPython模組上看到他用下面的方式做git binding, # 等同 git commint arg1 arg2 --opt1 --opt2 value git.commit(arg1, arg2, opt1=True, opt2=value) 基本上就是把 method 當成 sub command, kwargs 則 轉換成 option 我覺得滿好用的, 所以就把code出抽來寫成一個模組, 拿來寫寫系統管理工具時,還滿方便的 下面是簡單的範例 import ucltip ls = ucltip.SingleCmd('ls') # 等同 ls --all --almost-all print ls(all=True, almost_all=True) zenity = CmdDispatcher('zneity', opt_style=1, subcmd_prefix='--') # 等同 zneity --info --text=hi zneity.info(text="hi") 利用這種方式寫的 Zenity Binding, 只有少數幾種要對執行結果需要再做處理 https://github.com/hychen/vsgui/blob/master/vsgui/zenity.py ucltip module http://pypi.python.org/pypi/ucltip/ -- 身為一個阿宅, 在家工作也是理所當然的, 你說是吧! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.36.164.40
POSIX:push! 12/22 04:02