2024-10-01 13:26:32 发布
网友
我想用多个条件执行find,例如:findfoo排除隐藏文件:
find
find . -type f \( -iname '*foo*' ! -name '.*' \)
Python代码:
有人能解释一下我遗漏了什么吗?谢谢!在
至少,需要逃离那里。在
在第二步,或者通过反斜杠转义(将“\\(”和“\\)”传递给shell)
cmd = ["find", ".", "-type", "f", "\\(", "-iname", "\\*foo\\*", "!", "-name", ".\\*", "\\)"]
或者干脆把那些(和)扔掉-
应该可以正常工作
我也遇到过这个问题,我相信你现在已经知道了,但我想我会考虑一下,以防其他人遇到同样的问题。最后发现,这是由于Python在使用Popen时的实际操作(当使用shell=True时,Python基本上只是使用/bin/sh-c来传递命令(Python's subprocess.Popen() results differ from command line?)。默认情况下,shell为False,因此如果忽略此设置或将其设置为False,则将使用“executable”中指定的任何内容。文档将在此处进行更详细的说明:https://docs.python.org/2/library/subprocess.html#subprocess.Popen
沿着这条线应该能奏效
import subprocess cmd = 'find . -type f -iname "*foo*" ! -name ".*"' sp = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print sp.communicate()[0].split()
在Python3.7中子流程.运行()你可以把空格上的splitting命令放到一个列表中,字符串就可以了。在
文档中没有任何内容subrocess.run()。在
我无法让一个命令扩展到一个列表中工作,而字符串工作得很好。在
cmd = "find . -type f -iname \*foo\* ! -name .\\*" print(cmd) ret = subprocess.run(cmd, shell=True, capture_output=True) print(ret)
测试:
至少,需要逃离那里。在
在第二步,或者通过反斜杠转义(将“\\(”和“\\)”传递给shell)
或者干脆把那些(和)扔掉-
^{pr2}$应该可以正常工作
我也遇到过这个问题,我相信你现在已经知道了,但我想我会考虑一下,以防其他人遇到同样的问题。最后发现,这是由于Python在使用Popen时的实际操作(当使用shell=True时,Python基本上只是使用/bin/sh-c来传递命令(Python's subprocess.Popen() results differ from command line?)。默认情况下,shell为False,因此如果忽略此设置或将其设置为False,则将使用“executable”中指定的任何内容。文档将在此处进行更详细的说明:https://docs.python.org/2/library/subprocess.html#subprocess.Popen
沿着这条线应该能奏效
在Python3.7中子流程.运行()你可以把空格上的splitting命令放到一个列表中,字符串就可以了。在
文档中没有任何内容subrocess.run()。在
我无法让一个命令扩展到一个列表中工作,而字符串工作得很好。在
测试:
^{pr2}$相关问题 更多 >
编程相关推荐