find command with exec in python subprocess gives error

2024/11/15 22:57:19

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.

Answer

An answer on another question helped: https://stackoverflow.com/a/15035344/971529

import subprocesssubprocess.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 {} \;
https://en.xdnf.cn/q/71735.html

Related Q&A

Tensorflow import error on Pycharm (Mac)

Error msg (check the screenshot picture please):ImportError: cannot import name symbol_databaseError importing tensorflow. Unless you are using bazel, you should not try to import tensorflow from its …

ssl.SSLCertVerificationError for flask application OAuth login with keycloak

I have referred a sample hello-world flask app integrated with key-cloak login from https://gist.github.com/thomasdarimont/145dc9aa857b831ff2eff221b79d179a My client-secrets.json is as follows: {"…

Need to transfer multiple files from client to server

Im recently working on a project in which Im basically making a dropbox clone. The server and client are working fine but Im having a slight issue. Im able to transfer a single file from the client to …

pyplot bar charts with individual data points

I have data from a control and treatment group. Is matplotlib able to create a bar chart where the bar height is the mean of each group overlaid with the individual data points from that group? Id lik…

python only works with sudo

My python 2.7 script works on my Ubuntu system if I call it using sudo python [filename].pyor from a bash script using sudo ./[bashscriptname].shBut if I call it from Pycharm I get oauth errors, and fr…

Forbidden (CSRF token missing or incorrect) Django error

I am very new to Django. The name of my project is rango and I have created a URL named /rango/tagger that is supposed to send an object. In my java-script, I have tried to communicate with this route …

How to update artists in scrollable, matplotlib and multiplot

Im trying to create a scrollable multiplot based on the answer to this question: Creating a scrollable multiplot with pythons pylabLines created using ax.plot() are updating correctly, however Im unabl…

Sending Keys Using Splinter

I want to test an autocomplete box using Splinter. I need to send the down and enter keys through to the browser but Im having trouble doing this. I am currently finding an input box and typing tes int…

Split lists within dataframe column into multiple columns [duplicate]

This question already has answers here:Split a Pandas column of lists into multiple columns(13 answers)Closed 3 years ago.I have a Pandas DataFrame column with multiple lists within a list. Something l…

Drawing with turtle(python) using PyCharm

Im running the latest PyCharm Pro version and trying to run the below code from a scratch file but it doesnt seem to work import turtlewn = turtle.Screen() alex = turtle.Turtle() alex.forward(150) a…