Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7830633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:19:33+00:00 2026-06-02T11:19:33+00:00

I came across this great replacment for the getstatusoutput() function in Python 2.* which

  • 0

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 something wrong with the way the output is constructed. It only returns the last line of the output, but I can’t figure out why. Any help would be awesome.

def getstatusoutput(cmd):
    """Return (status, output) of executing cmd in a shell."""
    """This new implementation should work on all platforms."""
    import subprocess
    pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, universal_newlines=True)
    output = "".join(pipe.stdout.readlines())
    sts = pipe.returncode
    if sts is None: sts = 0
    return sts, output
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T11:19:34+00:00Added an answer on June 2, 2026 at 11:19 am

    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across this recommendation for reading the C++ report magazine. However, when I
I came across this great JQuery example of using a slidetoggle. I want to
I am using this IP Validation Function that I came across while browsing, it
First of all, it's great. However, I came across a situation where my benchmarks
I came across this recently and thought it would make a great SO question.
I came across a few articles like this one , which suggest that some
I'm working on my first JSON example in objective-c and came across this great
I came across this function of converting a SimpleXML Object to an array here
I came across materialized views, which look great: http://en.oreilly.com/mysql2011/public/schedule/detail/17146 However there is too much
I came across this block of code which pulls images from a folder specified

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.