简体   繁体   中英

Invoke SOLR post tool command using Python subprocess

I want to invoke this SOLR post command through Python subprocess:

/u01/tony/solr-7.5.0/bin/post -c techproducts /u01/tony/data/bbc/politics/*.txt -params "literal.keywords=politics"

I tried firing this using python subprocess module as follows:

subprocess.run(["/u01/tony/solr-7.5.0/bin/post -c techproducts /u01/tony/data/bbc/politics/*.txt -params", "literal.keywords=politics"], stdout=PIPE, stderr=PIPE)

But this throws the following error:

FileNotFoundError: [Errno 2] No such file or directory: '/u01/tony/solr-7.5.0/bin/post -c techproducts /u01/tony/data/bbc/politics/*.txt -params': '/u01/tony/solr-7.5.0/bin/post -c techproducts /u01/tony/data/bbc/politics/*.txt -params'

You need to split all your arguments - the first argument is the command to run.

["/u01/tony/solr-7.5.0/bin/post", "-c", "techproducts", "/u01/tony/data/bbc/politics/*.txt", "-params", "literal.keywords=politics"

Also, be aware that the *.txt expansion is done by your shell, so if the command is not invoked in a shell context (.. which it isn't here), it won't be expanded. However, since the bin/post tool accepts a directory as a direct parameter and has a -filetypes parameter, you can use

"-filetypes txt", "/u01/tony/data/bbc/politics/"

.. instead.

The bin/post tool is also a shell script, so if it doesn't allow direct invocation (I'm not sure how this is resolved), you might have to prepend the invocation array with

"/usr/bin/env", "bash"

as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM