Execute an external application from inside a python script

Quinn Dunkan quinn at ngwee.ugcs.caltech.edu
Sat Jan 27 00:56:23 EST 2001


On Fri, 26 Jan 2001 12:26:44 +0100, Arild Hansen <arildh at stud.cs.uit.no> wrote:
>Hello,
>
>How do I execute an (executable) application from inside some python code,

See os.system(), os.fork(), or os.spawnv()

>and how do I terminate this application? All help greatly appreciated.

Under unix, os.kill().  Under windows, I dunno.  Poke about in the win32
module documentation.

Unix example with os.spawnv (python2 feature):

import os

pid = os.spawnv(os.P_NOWAIT, '/bin/sleep', ('sleep', '500'))
... stuff ...
# time to kill it:
os.kill(pid, signal.SIGTERM)
wpid, status = os.waitpid(pid, 0)
signal = status & 0xff
status = status >> 8
print '%d killed by signal %d with status %d' %(wpid, signal, status)



More information about the Python-list mailing list