How to make execfile exit?

Piet van Oostrum piet at cs.uu.nl
Mon Apr 30 14:28:11 EDT 2001


>>>>> Campbell <cb921 at voice.co.za> (C) writes:

C> I've got a cute server now, for an incoming connection it identifies the
C> destination, and runs execfile on the correct python script.  My problem
C> is that once the file is being run, I can find no way to exit from the
C> execfile function unless the sys.exit() statement is executed in the
C> root of the script.  An example might excuse my explanation:

Your description is difficult to understand, but I guss that you mean to
catch the error from the main program.
In reality sys.exit raises the SystemExit exception, so you can catch this
one. Or any other exception that you use instead.

E.g. 

me = funkyclass()
try:
  execfile('destination.py')
except SystemExit, e:
  print e
print "execfile finished"

And it should be class funkyclass, not def.
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oostrum at hccnet.nl



More information about the Python-list mailing list