Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

commands.getstatusoutput on NT

61 views
Skip to first unread message

Lee, Jaeho

unread,
May 24, 2000, 3:00:00 AM5/24/00
to pytho...@python.org
I want to use commands.getstatusoutput on NT. I have some python script
works on Unix and I want to port it to NT. Except getstatusoutput,
everything is working fine. I made a simple test script.

import commands
r = commands.getstatusoutput("dir")
for s in r:
print s

But r has the following error message.

1
The name specified is not recognized as an
internal or external command, operable program or batch file.

What did I do wrong? On Unix, commands.getstatusoutput("ls") works fine. If
there is any other method that does same job on NT, I can change my script.
What I want to do is run shell command and get the result.

Thanks in advance,
Jaeho


Lee, Jaeho

unread,
May 24, 2000, 3:00:00 AM5/24/00
to Trent Mick
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


David Bolen

unread,
May 24, 2000, 3:00:00 AM5/24/00
to
"Lee, Jaeho" <Jh...@Brooks.com> writes:

The problem is that the commands module is Unix-specific. In this
case, getstatusoutput is doing an os.popen on a string of the form
"{ cmd; } 2>&1", which won't work under NT. I think the commands
module is documented in the Unix-specific chapter of the library
documentation (the module source also has a comment to that effect).

If you want to execute a command and get its output more portably,
you could probably just do the os.popen() call yourself,
conditionalizing the actual command more precisely than
commands.getstatusoutput permits. Since you've already got to
conditionalize ls versus dir somehow that shouldn't introduce too much
more complexity in terms of OS-handling. If you peek at the module,
you'll see that the source for commands.getstatusoutput is pretty simple.

For example, the command:

s,r = commands.getstatusoutput("dir")

could be replaced with:

pipe = os.popen("dir 2>&1")
r = pipe.read()
s = pipe.status()

where r is the output and s the status (None if no error). I didn't
bother simulating handling the return as a single list (as you have
when you just set it to r). You may or may not need the 2>&1, but
without it, the pipe will only return stdout from the child process.

It may be worth pointing out however, that while this can be a good
approach for arbitrary commands, in the particular case of a directory
listing (e.g., "ls" versus "dir") you may be better served - and more
portably - by using some other functions in the os module such as
os.listdir(), perhaps coupled with os.stat() to individual file
details.

--
-- David
--
/-----------------------------------------------------------------------\
\ David Bolen \ E-mail: db...@fitlinxx.com /
| FitLinxx, Inc. \ Phone: (203) 708-5192 |
/ 860 Canal Street, Stamford, CT 06902 \ Fax: (203) 316-5150 \
\-----------------------------------------------------------------------/

David Bolen

unread,
May 24, 2000, 3:00:00 AM5/24/00
to
David Bolen <db...@fitlinxx.com> writes:

(...)


> For example, the command:
>
> s,r = commands.getstatusoutput("dir")
>
> could be replaced with:
>
> pipe = os.popen("dir 2>&1")
> r = pipe.read()
> s = pipe.status()

Small correction to my prior post - the final line should read:

s = pipe.close()

0 new messages