使用子进程在 python 中发出 运行 查找命令
Issue running find command in python with subprocess
我知道这个问题已经被问过几次了,只是看不出我的用法有什么问题。
用法#1
proc = subprocess.Popen(['/usr/bin/find', '/path/to/dir', '-type', 'f', '-name', '"*.gradle"', '-exec', 'grep', '"KEYWORD"', '{}', '/dev/null', ';'], stdout=PIPE, stderr=PIPE)
output, error = proc.communicate()
Error: doesn't list any files.
用法#2
proc = subprocess.Popen(['/usr/bin/find', '/path/to/dir', '-type', 'f', '-name', '"*.gradle"', '-exec', 'grep', '"KEYWORD"', '{}', '/dev/null', '\;'], stdout=PIPE, stderr=PIPE)
output, error = proc.communicate()
Error: find: -exec: no terminating ";" or "+"
用法#3
proc = subprocess.Popen(['/usr/bin/find', '/path/to/dir', '-type', 'f', '-name', '"*.gradle"', '-exec', 'grep', '"KEYWORD"', '{}', '/dev/null', '\;'], stdout=PIPE, stderr=PIPE)
output, error = proc.communicate()
Error: find: -exec: no terminating ";" or "+"
我可以让命令与 shell=True 选项一起使用。但是,希望避免将其作为最佳做法。
当来自 shell 的 运行 时,命令工作正常。
/usr/bin/find /path/to/dir -type f -name "*.gradle" -exec grep "KEYWORD" {} /dev/null \;
Python版本:2.7.11
OS X 10.11.3
感谢任何关于如何让它工作的指示。
试试这个,
cmd='/usr/bin/find /path/to/dir -type f -name "*.gradle" -exec grep "KEYWORD" {} /dev/null \;'
proc = subprocess.Popen(cmd.split(), stdout=PIPE, stderr=PIPE)
output, error = proc.communicate()
当您将命令构建为与 Popen
一起使用的列表时,您不需要使用 shell 转义符,因此在这种情况下 \;
将被解释为文字反斜杠后跟一个分号,而 find
期望只看到一个分号作为单个参数。此外 "KEYWORD"
将包含引号,因此在没有引号的情况下找不到 KEYWORD
。类似于 "*.gradle"
,它只会匹配引号中的文件名。
proc = subprocess.Popen(['/usr/bin/find', '/path/to/dir', '-type', 'f',
'-name', '*.gradle', '-exec', 'grep', 'KEYWORD',
'{}', '/dev/null', ';'],
stdout=PIPE, stderr=PIPE)
我知道这个问题已经被问过几次了,只是看不出我的用法有什么问题。
用法#1
proc = subprocess.Popen(['/usr/bin/find', '/path/to/dir', '-type', 'f', '-name', '"*.gradle"', '-exec', 'grep', '"KEYWORD"', '{}', '/dev/null', ';'], stdout=PIPE, stderr=PIPE)
output, error = proc.communicate()
Error: doesn't list any files.
用法#2
proc = subprocess.Popen(['/usr/bin/find', '/path/to/dir', '-type', 'f', '-name', '"*.gradle"', '-exec', 'grep', '"KEYWORD"', '{}', '/dev/null', '\;'], stdout=PIPE, stderr=PIPE)
output, error = proc.communicate()
Error: find: -exec: no terminating ";" or "+"
用法#3
proc = subprocess.Popen(['/usr/bin/find', '/path/to/dir', '-type', 'f', '-name', '"*.gradle"', '-exec', 'grep', '"KEYWORD"', '{}', '/dev/null', '\;'], stdout=PIPE, stderr=PIPE)
output, error = proc.communicate()
Error: find: -exec: no terminating ";" or "+"
我可以让命令与 shell=True 选项一起使用。但是,希望避免将其作为最佳做法。
当来自 shell 的 运行 时,命令工作正常。
/usr/bin/find /path/to/dir -type f -name "*.gradle" -exec grep "KEYWORD" {} /dev/null \;
Python版本:2.7.11
OS X 10.11.3
感谢任何关于如何让它工作的指示。
试试这个,
cmd='/usr/bin/find /path/to/dir -type f -name "*.gradle" -exec grep "KEYWORD" {} /dev/null \;'
proc = subprocess.Popen(cmd.split(), stdout=PIPE, stderr=PIPE)
output, error = proc.communicate()
当您将命令构建为与 Popen
一起使用的列表时,您不需要使用 shell 转义符,因此在这种情况下 \;
将被解释为文字反斜杠后跟一个分号,而 find
期望只看到一个分号作为单个参数。此外 "KEYWORD"
将包含引号,因此在没有引号的情况下找不到 KEYWORD
。类似于 "*.gradle"
,它只会匹配引号中的文件名。
proc = subprocess.Popen(['/usr/bin/find', '/path/to/dir', '-type', 'f',
'-name', '*.gradle', '-exec', 'grep', 'KEYWORD',
'{}', '/dev/null', ';'],
stdout=PIPE, stderr=PIPE)