在python子进程中用exec找到命令给出错误

编程入门 行业动态 更新时间:2024-10-26 14:30:57
本文介绍了在python子进程中用exec找到命令给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

/ usr / bin / find< / p>

我试图使用子进程模块(python)执行以下命令。文件路径> -maxdepth 1 -type f -iname< pattern> -exec basename {} \;

但是,它给出了以下错误: < pre $ / usr / bin / find:缺少参数到`-exec'

我猜这是为了逃避某些角色。但没有得到如何克服这一点。

任何帮助表示赞赏。谢谢。

解决方案

另一个问题的答案帮助: stackoverflow/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: stackoverflow/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找到命令给出错误

本文发布于:2023-08-05 05:40:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1303007.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中用   进程   命令   错误   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!