How do I run an external system command on Windows?

David Bolen db3l at fitlinxx.com
Mon Sep 18 18:47:49 EDT 2000


noahspurrier at my-deja.com writes:

> Wait! I take it back, because os.system() does not return you the
> result
> as a string. You get the exit code, but all output is lost. I suppose I
> could redirect it to a file and then read it in, but that would not be
> much fun.

Well, but that does match the Perl "system" command which is one of
the items you asked about :-)

But you're right it doesn't work like Perl's backtick (`) operator,
which returns the result of the execution.

> I want the functionality of the commands module on UNIX <b>and</b>
> Windows NT -- Or even just WinNT! Now I can't figure out how
> to do it under Windows...

Yeah, the commands module currently doesn't work under NT since it
makes some shell and command syntax assumptions.

In general, you can execute a command and retrieve it's output with
the os.popen() function.  But prior to 2.0, if you want reliability
under all Windows platforms and environments (console and graphical),
you should use the win32pipe module from the Win32 extensions.  So if
you want cross-platform with Python 1.5.2 (or I believe 1.6 as well)
you should probably just check the platform, and select either
os.popen() or win32pipe.popen() based on non-NT or NT.

The win32pipe functionality has been rolled into the os.popen*()
functions in Python 2.0, so with 2.0 you can just use the os module
and be portable across platform.

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



More information about the Python-list mailing list