subprocess vs. proctools
nick at craig-wood.com
Wed Dec 15 14:30:01 CET 2004
Keith Dart <kdart at kdart.com> wrote:
> Nick Craig-Wood wrote:
> > This sounds rather like the new subprocess module...
> >>>>import subprocess
> >>>>rc = subprocess.call(["ls", "-l"])
> > total 381896
> > -rw-r--r-- 1 ncw ncw 1542 Oct 12 17:55 1
> > [snip]
> > -rw-r--r-- 1 ncw ncw 713 Nov 16 08:18 z~
> >>>>print rc
> > 0
> But this evaluates to False in Python, but True in a shell.
There are many ways for a program to fail (non-zero exit codes) but
only one way for it to succeed (zero exit code). Therefore rc should
be 0 for success.
IMHO Shell semantics are nuts (0 is True - yeah!) - they hurt my head
every time I have to use them ;-)
> It also requires an extra check for normal exit, or exit by a
>>> import subprocess
>>> subprocess.call(["sleep", "60"])
# I killed the sleep process with a SEGV here from another xterm
>>> subprocess.call(["sleep", "asdfasdf"])
sleep: invalid time interval `asdfasdf'
Try `sleep --help' for more information.
Signals are -ve, exit codes are +ve which seems perfect. Exit codes
can only be from 0..255 under linux. Signals go from -1 to -64.
> The proctools ExitStatus object avaluates to True only on a normal
> exit, period. Thus it follows a shell semantics for clarity. You
> cannot do this with the subprocess module:
> if rc:
> print "exited normally"
Actually I think
if rc == 0:
print "exited normally"
is exactly equivalent!
> It does not work with MS Windows
I like python because I can write stuff on linux and it works on
windows without too much effort, and in general I try not to use
modules which don't work on both platforms.
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick
More information about the Python-list