[英]Capture output of complex shell command in python
我想在python腳本中嵌入命令並捕獲輸出。 在這種情況下,我嘗試使用"find"
在不確定數目的子目錄中查找不確定數目的文件,並為字符串匹配每個匹配文件,例如:
grep "rabbit" `find . -name "*.txt"`
我正在運行Python 2.6.6(是的,我也很抱歉,但是現在不能為此讓整個組織失望)。
我已經嘗試了一堆東西使用subprocess
, shlex
等已建議在這里,但我還沒有找到一種語法,要么苦難言,或者最終吮吸“查找......” as the search string for
grep`等的as the search string for
。建議表示贊賞。 肯
import subprocess
find_p = subprocess.Popen(["find", ".", "-name", "*.txt"], stdout=subprocess.PIPE)
grep_p = subprocess.Popen(["xargs", "grep", "rabbit"], stdin=find_p.stdout)
grep_p.wait()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.