由于某些原因,我将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