gpt4 book ai didi

对于 find 命令,Python 子进程未按预期工作

转载 作者:太空狗 更新时间:2023-10-29 11:30:57 26 4
gpt4 key购买 nike

我试图在 python 中找出节点上的孤立文件。下面是代码片段

#!/usr/bin/python
import subprocess
try:
s = subprocess.check_output(["find", "/", "-fstype", "proc", "-prune", "-o", "\( -nouser -o -nogroup \)", "-print"])
except subprocess.CalledProcessError as e:
print e.output
else:
if len(s) > 0:
print ("List of Orphan Files are \n%s\n" % s)
else:
print ("Orphan Files does not Exists on the NE")

当我尝试运行这段 python 代码时

> python test.py 
find: paths must precede expression: \( -nouser -o -nogroup \)
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

当我在 CLI 上运行相同的命令时,它工作正常。

> find / -fstype proc -prune -o \( -nouser -o -nogroup \) -print
/root/a

很少有人建议使用 shell=true,根据子进程的 python 文档,这是安全隐患。警告使用 shell=True 可能存在安全隐患。

最佳答案

只需将 shell=True 添加到您的 check_output

s = subprocess.check_output(["find", "/", "-fstype", "proc", "-prune", "-o", "\( -nouser -o -nogroup \)", "-print"], shell=True)

关于对于 find 命令,Python 子进程未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54625736/

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