commands.getstatusoutput on NT

Lee, Jaeho Jhlee at Brooks.com
Wed May 24 18:10:31 EDT 2000


Trent Mick wrote:
>THe commands module is actually using the following code:
>
>
>def getstatusoutput(cmd):
>    """Return (status, output) of executing cmd in a shell."""
>    import os
>    pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
>    text = pipe.read()
>    sts = pipe.close()
>    if sts == None: sts = 0
>    if text[-1:] == '\n': text = text[:-1]
>    return sts, text
>
>i.e. instead of running "dir" is runs "{ dir; } 2>&1" on the 
>NT shell. NT's
>shell does not understand the braces. You can manually use the 
>same code but
>drop the braces (and the semi-colon for that matter). The '2>&1' copies
>stderr to stdout so that both streams are captures.
>
>Note that os.popen (i.e. shell pipes) are, I believe, not 
>considered fully
>reliable on NT (and certainly not on Win9x). The safest way to 
>do all this is
>to redirect shell output to a file and then just process it yourself.
>Granted that this is a pain.
>
>And, by the way, os.listdir() is a better way to get a list of 
>the files in a
>directory. But then you gave a test case and you know that.
>
>Hope this helps,
>
>Trent 
>

Thanks Trent,

I used the code and made my local getstatusoutput. It works fine. You
guessed right. I need more than just 'dir'. It is for preparing test, such
as removing garbage file. But I want to user can set the preparation task in
the test script. Because this is not critical stuff, I think that I can stay
with popen, unless it does not make big disaster.

I copied this mail to python-list so that any other people can get help from
your mail.

Thanks,
Jaeho Lee




More information about the Python-list mailing list