[Tutor] execute an OS command, get the output

Terry Carroll carroll at tjc.com
Sat Mar 11 01:43:41 CET 2006


I need to execute a command and capture the stdout output.  I'm
overwhelmed by the plethora of means that Python offers to do something
like this, and don't know which, if any, is most applicable:

1) the os.system module
2a-d) os.popen, and popen2 popen3 and popen4
3) the popen2 module
4) the subprocess module


#1 is synchronous, which is what I want, but it doesn't seem to have any 
means to capture stdout.

#s 2-4 look like they support capturing stdout, but they seem to be 
asynchronous, which is at the very least overkill for me.

I would ideally like something like this:

    abc = executeit(commandline)    or
    executeit(commandline, abc)

where, after execution, abc be a string or list that would contain the
output of the command line commandline after its completed.  I'm not
worried too much about syntax, though; I'd be happy to just find some
synchronous command-exeutor that gives me the standard output by any
means.

More detail, in case it matters:  the command line I need to execute is 
actually three commands, with pipes between them, i.e.,

  string xxxxxxx | grep zzzzzz | head -10

My environment is Python 2.4, Windows/XP (with cygwin installed, hence the 
availablility of the string, grep and head commands)



More information about the Tutor mailing list