views:

312

answers:

4

I have a command that works great on the command line. It has lots of arguments like cmd --thing foo --stuff bar -a b input output

I want to run this from python and block waiting for it to complete. As the script prints things to stdout and stderr I want it to be immediately shown to the user.

What is the right module for this?

I've tried:


import commands
output = commands.getoutput("cmd --thing foo --stuff bar -a b input output")
print output

this works great except the stdout isn't returned until the end.


import os
os.system("cmd --thing foo --stuff bar -a b input output")

this prints all the output when the cmd is actually finished.


import subprocess
subprocess.call(["cmd", "--thing foo", "--stuff bar", "-a b", "input", "output"])

this doesn't pass the parameters correctly somehow (I haven't been able to find the exact problem, but cmd is rejecting my input). If I put echo as the first parameter, it prints out the command which works perfectly when I paste it directly into the terminal.


import subprocess
subprocess.call("cmd --thing foo --stuff bar -a b input output")

exactly the same as above.

+7  A: 

You have to quote each field separately, ie. split the options from their arguments.

import subprocess
output = subprocess.call(["cmd", "--thing", "foo", "--stuff", "bar", "-a", "b", "input", "output"])

otherwise you are effectively running cmd like this

$ cmd --thing\ foo --stuff\ bar -a\ b input output

To get the output into a pipe you need to call it slightly differently

import subprocess
output = subprocess.Popen(["cmd", "--thing", "foo", "--stuff", "bar", "-a", "b", "input", "output"],stdout=subprocess.PIPE)
output.stdout   #  <open file '<fdopen>', mode 'rb'>
gnibbler
`subprocess` doesn't seem to be printing the output as it is returned. Can I make that happen?
Paul Tarjan
@Paul Tarjan: That's a duplicate question. Please search for `"[python] subprocess"` and you'll get dozens of answers to that question.
S.Lott
+2  A: 

Wouldn't commands.getstatusoutput() work? It'll return your status right away pretty sure.

The Jug
It does, but be warned that it is POSIX only (no Windows).
jathanism
+1  A: 

A coworker just showed me this:

import os
import sys
for line in os.popen("cmd --thing foo --stuff bar -a b input output", "r"):
    print line
    sys.stdout.flush()

and it is working :)

Paul Tarjan
You should use `subprocess` in preference to `popen`. For one thing it saves you worrying about escaping parameters
gnibbler
Another reason: Since Python 2.6 `os.popen()` is classified as deprecated in favor of `subprocess` (see http://docs.python.org/library/os.html#os.popen).
Oben Sonne
+3  A: 

If you don't need to process the output in your code, only to show it to the user as it happens (it's not clear from your Q, and it seems that way from your own self-answer), simplest is:

rc = subprocess.call(
    ["cmd", "--thing", "foo", "--stuff", "bar", 
     "-a", "b", "input", "output"])
print "Return code was", rc

i.e., just avoid any use of pipes -- let stdout and stderr just show on the terminal. That should avoid any problem with buffering. Once you put pipes in the picture, buffering generally is a problem if you want to show output as it happens (I'm surprised your self-answer doesn't have that problem;-).

For both showing and capturing, BTW, I always recomment pexpect (and wexpect on Windows) exactly to work around the buffering issue.

Alex Martelli
As always, thank you Alex.It seems the `subprocess.call()` buffers the output of the other program, which is NOT the behavior that usually happens on the terminal for this program. Should I just do a `proc = subprocess.Popen` and then a for loop on `proc.stdout` printing the output?
Paul Tarjan
@Paul, it's not `subprocess` doing the buffering -- it's the other program's C runtime library doing it when it notices it's going to a pipe rather than a terminal (what you observe is surprising since stdout _is_ going to terminal so the buffering shouldn't happen). `pexpect` uses pseudo-terminals to fool the other program's runtime libraries and so stop them from buffering (`wexpect` on Windows) -- I've recommended them many, many times, pls search for my other answers for their URLs.
Alex Martelli