By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,537 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,537 IT Pros & Developers. It's quick & easy.

How to terminate a main script?

P: n/a
Hi,

I'm still looking for an elegant and clear means to
terminate the main script in Python.

Unfortunately, Python doesn't allow a 'return'
instruction in the main script.

Using sys.exit(0) produces an error
message which looks dangerous to an
uninitiated user.

The same is true for an exception.

And setting a 'flag' and testing
at several place for 'fall through'
is ugly and error-prone.

So what is a good choice?

Many thanks for a hint,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
Jul 11 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Helmut Jarausch wrote:
Using sys.exit(0) produces an error
message which looks dangerous to an
uninitiated user.
sys.exit(0) doesn't print anything at all.

$ python
>>import sys
sys.exit(0)
$

however, sys.exit() raises an exception to tell the runtime that it wants
to terminate the program, so if you're using a catch-all exception handler
that treats all exceptions as dangerous errors, it'll look like a dangerous
error too.

the solution is simple: don't do that. instead of writing:

try:
...
except:
print "OMG! Ponies!"

write

try:
...
except (SystemExit, KeyboardInterupt):
raise
except:
print "OMG! Ponies!"

</F>

Jul 11 '06 #2

P: n/a
>>>>Helmut Jarausch <ja******@igpm.rwth-aachen.dewrites:
Using sys.exit(0) produces an error message which looks dangerous to an
uninitiated user.
What message? Your program should exit silently when you call sys.exit(0).

Ganesan

--
Ganesan Rajagopal

Jul 11 '06 #3

P: n/a
Fredrik Lundh wrote:
Helmut Jarausch wrote:
>Using sys.exit(0) produces an error
message which looks dangerous to an
uninitiated user.

sys.exit(0) doesn't print anything at all.
Yes, sorry, I was trying in in 'idle'
There you get
Traceback (most recent call last):
File "<pyshell#1>", line 1, in -toplevel-
sys.exit(0)
SystemExit: 0
--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
Jul 11 '06 #4

P: n/a
Fredrik Lundh wrote:
Helmut Jarausch wrote:
>Using sys.exit(0) produces an error
message which looks dangerous to an
uninitiated user.

sys.exit(0) doesn't print anything at all.
Yes, sorry, I was trying in in 'idle'
There you get
Traceback (most recent call last):
File "<pyshell#1>", line 1, in -toplevel-
sys.exit(0)
SystemExit: 0
--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany

Jul 11 '06 #5

P: n/a
Helmut Jarausch wrote:
Hi,

I'm still looking for an elegant and clear means to
terminate the main script in Python.

Unfortunately, Python doesn't allow a 'return'
instruction in the main script.
It is quite a common practice for Python scripts to define a main()
function which contains the actual code, and have "main()" on the last
line of the file. This allows you to just 'return' from wherever you
like in your code.

It is even more common to use this in the end instead of "main()":
if __name__ == "__main__":
main()

This way, if the file is imported as a module, the main() function
won't execute, but if it is run directly from a command line or using
execfile() it will execute.

Enjoy!

- Tal

Jul 12 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.