What is a convenient way to execute a shell command in Python and retrieve its output?
-
The commands module seems like the way to go. The subprocess module doesn't seem very intuitive to use. Are there any other options?
-
Answer:
subprocess is the most common idiom in my experience, replacing the old popen2().
Jeff Hammerbacher at Quora Visit the source
Other answers
Be very careful using the commands module, or anything that takes a string and not a list of arguments. Functions that take a string generally end up running that string through your shell. If the command you're running involves any user input, all you need is a quote, a dollar sign, a semicolon, or any of the other standard forms of injection. Even with no user input, code gets copied around and reused and evolves over time, so a variable that didn't contain spaces before might start to. I recommend always using the subprocess module, because you know that with a list of arguments, they don't get processed at all before going to the actual command you're trying to run. If you're using Python 2.7 or newer, somebody finally added a subprocess.check_output function, which does exactly what you're looking for.
Evan Broder
I think you've already figured it out: the commands module is probably the way to go. >>> commands.getoutput('hostname') 'kashi.local' I don't believe subprocess is entirely unintuitive though. The module just allows for more fine-grained control of the process than a simple getoutput(). For example, separating out STDIN from STDERR: >>> subprocess.Popen('hostname', stdin=PIPE, stdout=PIPE).communicate() ('kashi.local\n', None)
Albert Sheu
The easiest way I'm aware of is to use commands.getoutput. >>> commands.getoutput('ls /bin/ls') '/bin/ls' http://docs.python.org/library/commands.html#commands.getoutput
Charlie Cheever
The more general utility is subprocess.Popen. Use stdout=subprocess.PIPE, stderr=subprocess.PIPE as kwargs, and then you can read from .stdout/.stderr. (Or use communicate().)
Philip Zeyliger
One of the ways is to use os.popen(). The code below executes "ls" command on the terminal and prints the results: import os f = os.popen(âls -lâ) for line in f.readlines(): print line
Prateek Joshi
Subprocess is definitely the way to go for flexibility and control, but here's quick and dirty: >>> print os.popen('pwd').readlines() ['/root\n']
Peter Merelis
When I'm using Python 2, I make a little function like this: def run(command): """Run command and return (output, exitCode). output is stdout/stderr combined via os.popen3 """ fin, fout = os.popen3(command) fin.close() output = fout.read() exitCode = fout.close() return output, exitCode For Python 3, clearly, subprocess is the way to go. Forget about whether it's intuitive. Use it a while. The beauty of the design is only now dawning on me.
Mark McEahern
subprocess is far worse than the simple commands that it replaced. However: http://pypi.python.org/pypi/iterpipes/ is a wrapper around that raw wound that is subprocess. Try it.
Kevin Beckford
https://github.com/kennethreitz/envoy is a wrapper library around the subprocess module.
Sushant Srivastava
Related Q & A:
- What is a good way to advertise enexpensively for a small business? It is handyman work?Best solution by Yahoo! Answers
- What would be a creative way to make a timeline?Best solution by Yahoo! Answers
- What's a good way to let a girl know you are interested in her?Best solution by Yahoo! Answers
- What is a good way to promote a movie?Best solution by Yahoo! Answers
- What is a creative way to display a family tree?Best solution by geni.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.