Shell command not working from python, ok from shell

I have a python script that generates several shell commands from a given input. The problem is that when he tries to execute the generated commands, it fails, but when I run the generated commands myself (i.e. from the command line), they are executed successfully.

Here is the generated command:

find /home/me/downloader/0-29/ -type f | grep -i .rpm$ | xargs -i cp {} /home/me/downloader/builds/0-29/

Here is the error message when it is launched using a python script:

find: paths must precede expression: |
Usage: find [-H] [-L] [-P] [-Olevel] [-D help | tree | search | stat | rates | opt | exec] [path ...] [expression]

Could you help me understand what the problem is?

UPD : here is the function that I use to execute the generated commands:

def exec_command(command):
        process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
        output = process.communicate()[0]
        return output
+3
source share
2

, shell=True, , , :

command = 'find /home/me/downloader/0-29/ -type f | grep -i .rpm$ | xargs -i cp {} /home/me/downloader/builds/0-29/'
subprocess.call(command, shell=True)

process = subprocess.Popen(command, shell=True)
output = process.communicate()[0]
return output

, python . , find | , .

, :

command="find /home/me/downloader/0-29/ -type f -iname '*.rpm' -exec cp {} /home/me/downloader/builds/0-29/ \;"

, shell = False. , '*.rpm' glob . shell = False . , . = False command.split():

command="find /home/me/downloader/0-29/ -type f -iname *.rpm -exec cp {} /home/me/downloader/builds/0-29/ \;"
+2

, find , . | grep xargs - .

, , , , bash find, grep .. - bash (-c) , ., ,

process = subprocess.Popen(["bash", "-c", "cat /etc/issue | grep a"], stdout=subprocess.PIPE)
output=process.communicate()[0]
print output
+1

All Articles