"shell-commands" and python!

Greg Weeks weeks at vitus.scs.agilent.com
Fri Sep 21 11:50:12 EDT 2001


Ignacio Vazquez-Abrams (ignacio at openservices.net) wrote:
: What, os.system('mycmd') isn't simple enough? Yeesh! Programmers these days...

But what about the following program invocation perks that we want to be as
easy in Python as they are in Ksh?:

    <  >  >>  2>  2>>  2>&1		# REDIRECTION
    &					# BACKGROUND
    |					# PIPING
    $() or ``				# CAPTURING STDOUT

Of course we can go the "open pipe dup2 close fork execv waitpid" route,
but that is far more verbose.  My own viewpoint, adopted ruefully based on
the current situation, is to bless the practice of invoking sh or ksh from
Python and let the shell handle the above goodies.

Indeed, on my system, system.getstatusoutput and os.system already do this.
The only problem is to not be ashamed to exploit it.

(Actually, there is another problem.  I think it is tacky -- downright
Perlish -- to check repeatedly for bad exit codes.  I'd prefer program
invocations with bad exit codes to throw an exception that includes both
the exit code and the text from stderr.  Happily, this is easy enough to
implement as a utility.)

Greg



More information about the Python-list mailing list