2014-07-12 39 views
0

我試圖測試python並使用這個命令。 Iam試圖模擬下面的命令行。在python上測試grep -r使用子進程.Popen

grep -r "any string" . -B1 or grep -r --include=*.xml "any string" -B1

#!/usr/bin/env python 
import subprocess 

#subprocess.call("ls -l", shell=True) 


message_type = raw_input('Enter Something:') 
print'"{}"'.format(message_type) 
subprocess.Popen(['grep -r', message_type, '.', '-B1'], shell=True) 
+1

這是什麼問題? – ZenOfPython

+0

它的工作現在我對待grep -r作爲一個我應該分開它並切換shell = False – nubianz

回答

1

指定grep-r作爲分隔的項目:

subprocess.Popen(['grep', '-r', message_type, '.', '-B1'], shell=True) 

否則grep -r被認爲是作爲一個程序而不是grep

+0

使用此修復程序,'shell = False'會更有意義。 – tripleee

+0

不錯,它確實抓住了forktru的loc和開關shell = False – nubianz