由於某些原因,我將bash腳本翻譯成python。在Python中做shell反引號?
Python是功能更強大,但是,它是更困難這樣的代碼簡單的bash代碼:
MYVAR = `grep -c myfile`
與Python我必須先定義一個反引號功能可能是:
def backquote(cmd,noErrorCode=(0,),output=PIPE,errout=PIPE):
p=Popen(cmd, stdout=output, stderr=errout)
comm=p.communicate()
if p.returncode not in noErrorCode:
raise OSError, comm[1]
if comm[0]:
return comm[0].rstrip().split('\n')
很無聊!
是否有Python的味道(IPython?),它很容易產生進程並獲取輸出?
++,不知道這個函數 – 2011-03-21 12:18:02