[英]How do I execute a complex “find” linux shell command in python
我需要執行以下命令:
find PATH -type f -exec du -h --all {} +
這是我嘗試這樣做的:
import subprocess
result = subprocess.Popen(["find", PATH, "-type", "f", "-exec", "du", "-h", "--all", "{}", "+"], shell=True, stdout=subprocess.PIPE).communicate()[0]
print(result)
結果我得到了一些垃圾。 我究竟做錯了什么?
當Popen()
的第一個參數是一個應該由shell解析的字符串時,你應該只使用shell=True
。 如果它是一個數組,你已經完成了必要的解析,不應該使用shell=True
。
import commands
commands = r'''find PATH -type f -exec du -h --all {} +'''
result = commands.getstatusoutput(command)[0]
print("{}".format(result))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.