This is not to say that all the information isn't available in the official documentation. In fact, it is. It's just not where I always look for it. The first thing I look for are examples. I simply do not have time to read the entire documentation page for something which should not be too difficult.
The basic idea is this:
- Either provide a string of the entire command and argument, but use Shell=True
- Provide a string for only the command
- Provide a list of strings, where the first item is the command and the other items are the arguments
1 #!/bin/env python 2 import subprocess 3 4 program_name = "ls" 5 arguments = ["-l", "-a"] 6 7 command = [program_name] 8 command.extend(arguments) 9 10 output = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0] 11 print output
Passing Arguments with Quotes
If you want to pass arguments that are passed with quotes in the shell, then just pass them as a single list item, without the quotes.
Links
- James Gardner wrote the most comprehensive introduction to the python subprocess module I've encountered.
- The official documentation.
Further Reading
I got stuck on argument passing with subprocess as well, but this helped. Thanks!
ReplyDeleteThis helped me out. Thanks!
ReplyDeleteThis is great. Thank you! I beat my head over trying to get this to work, for hours, and used the string way --- until it broke. Coming back here, I finally got the 'right' way working.
ReplyDeleteOne thing to watch out for (in my case, I was doing pdftk) is that call commands MUST be broken apart. The 'cat output' in pdftk was killing me; it has to be 'cat', 'output'. This is obvious in hindsight, but it's the simple stuff... ;-)
It's great you took the time to dig through the documentation and share it, it saved me some time and frustration, thanks!
ReplyDeleteCommand can also be composed in 1 line:
ReplyDeletecommand = ["openssl", "x509", "-in", sys.argv[1], "-text", "-noout"]
Thank you !
ReplyDelete