在python子进程中用exec找到命令给出错误 [英] find command with exec in python subprocess gives error
问题描述
/ usr / bin / find< / p> 我试图使用子进程模块(python)执行以下命令。文件路径> -maxdepth 1 -type f -iname< pattern> -exec basename {} \;
但是,它给出了以下错误:
< pre $ / usr / bin / find:缺少参数到`-exec'
我猜这是为了逃避某些角色。但没有得到如何克服这一点。
任何帮助表示赞赏。谢谢。
另一个问题的答案帮助:
https://stackoverflow.com/a/15035344/971529
import subprocess
subprocess.Popen(('find','/ tmp / mount','-type','f',
'-name','* .rpmsave',' -exec','rm','-f','{}',';'))
我无法弄清楚的是,分号不需要转义,因为通常分号是由bash解释的,需要被转义。
在bash中,这个等价物是:
$ b $ pre $ find $ / tmp / mount -type f -name* .rpmsave -exec rm -f {} \;
I'm trying to execute the following command using subprocess module (python)
/usr/bin/find <filepath> -maxdepth 1 -type f -iname "<pattern>" -exec basename {} \;
But, it gives the following error :
/usr/bin/find: missing argument to `-exec'
I am guessing it's to do with escaping some characters. But not getting how to get over this.
Any help is appreciated. Thanks.
An answer on another question helped: https://stackoverflow.com/a/15035344/971529
import subprocess
subprocess.Popen(('find', '/tmp/mount', '-type', 'f',
'-name', '*.rpmsave', '-exec', 'rm', '-f', '{}', ';'))
The thing I couldn't figure out was that the semi-colon didn't need to be escaped, since normally the semi-colon is interpreted by bash, and needs to be escaped.
In bash this equivelent is:
find /tmp/mount -type f -name "*.rpmsave" -exec rm -f {} \;
这篇关于在python子进程中用exec找到命令给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!