[英]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.