gpt4 book ai didi

python - 如何在 python 中执行复杂的 "find"linux shell 命令

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:46:06 24 4
gpt4 key购买 nike

我需要执行以下命令:

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

关于python - 如何在 python 中执行复杂的 "find"linux shell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45078097/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com