I came across this great replacment for the getstatusoutput()
function in Python 2.* which works equally well on Unix and Windows. However, I think there is someth
There is a bug in this.
The call:
pipe = subprocess.Popen(...)
kicks off the process. The line:
output = "".join(pipe.stdout.readlines())
reads the process's stdout (if there were error output it could get jammed up, but since you did not redirect stderr this is safe; in the more general case, you need to use the .communicate()
function to avoid possible wedging). You might think: well, now that I've read all the output, the subprocess has clearly finished, so pipe.returncode
should be set. But in fact, if you replace:
sts = pipe.returncode
if sts is None: sts = 0
with code that includes a diagnostic of some sort (or just delete the second line entirely), you'll find that, at least on some systems sometimes, sts
is None
. The reason is that the subprocess
module has not had a chance to retrieve it yet. You should replace those lines with:
sts = pipe.wait()
to collect the result and obviate the need for the is None
test. (It's safe to call .wait()
even if you've used .communicate()
and/or already called .wait()
.)
The "".join(...)
can be done slightly more efficiently by using just:
output = pipe.stdout.read()
All that said, it does get the full output for me. Perhaps you're reading from something that uses just '\r'
for newlines and your Python was built without universal-newline support? (If I run something that produces \r
-for-newline I still get all the lines, separated by actual newlines.)