Python reference

Doug Holton insert at spam.here
Fri Jun 4 06:11:45 EDT 2004


Here's an easy wrapper for os.spawn that lessens the confusion:

import os

def run (program, *args):
   "Run external program, wait til it quits, and return the exit code"
   spawn = os.spawnvp #not available on windows though
   if os.name == "nt": spawn = os.spawnv
   return spawn(os.P_WAIT, program, (program,) + args)

def start (program, *args):
   "Start an external program and return immediately, returning proc id"
   spawn = os.spawnvp #not available on windows though
   if os.name == "nt": spawn = os.spawnv
   return spawn(os.P_NOWAIT, program, (program,) + args)

#example:
errcode = run('cp','index.html','index2.html')
if errcode == 0: #no error
   pid = start('cp','index2.html','index3.html')
else:
   print "error finding or copying the index.html file"



More information about the Python-list mailing list