![](/img/trans.png)
[英]run a shell command in the background and pipe stdout to a logfile with python
[英]How to run shell command xargs with pipe in Python 2.6
我真正想要運行的是帶有xargs和管道的shell命令,如Python 2.6中所示,
grep -lsr "somestring" ./somedir | xargs rm
我想刪除./somedir
中所有內容匹配somestring
。
我嘗試了以下代碼,但無法正常工作。 感謝您的任何投入。
import subprocess
p1 = subprocess.Popen('grep -lsr "somestring" ./somedir', stdout=subprocess.PIPE, shell=True)
p2 = subprocess.Popen('xargs rm', stdin=p1.stdout, stdout=subprocess.PIPE, shell=True)
p1.stdout.close()
output = p2.communicate()[0]
我得到的錯誤:
rm: missing operand
Try `rm --help' for more information.
當使用shell = True時,Popen的第一個參數應該只是字符串而不是列表。
你也可以跑
subprocess.check_call("grep -lsr "somestring" ./somedir | xargs rm", shell=True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.