473,326 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How to terminate a main script?

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
5 36388
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
>>>>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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Shubhra Gupta | last post by:
> Hi, > > I have the following statement in one my python script :- > > assert not opt_initialize or os.path.exists(initdir), "initialize implies init directory" > > Could u tell please what...
5
by: vishal | last post by:
Hi, I am writing my first script to execute a Oracle procedure through a unix script. Could you tell me how do I do error handling here? How do I know in my script if the procedure executed...
17
by: CES | last post by:
All, I was wondering if their is a way of loading an external script fill from within a script?? <script language="javascript" type="text/javascript"> function test(var){ <script...
6
by: Francois Bonzon | last post by:
Any idea how I can launch a background task from a PHP script? For example, when a user posts on my message board, it may fire many e-mail notifications to other users, and other tasks. I want...
9
by: adamalton | last post by:
I've got a script which uses cURL to connect to another website, and it seems to be getting stuck (I can't quite figure out how, but that's besides the point!). The trouble is that when my script...
0
by: Gros Bedo | last post by:
Thank you guys for your help. My problem is that I project to use this command to terminate a script when uninstalling the software, so I can't store the PID. This command will be integrated in the...
0
by: Nigel Rantor | last post by:
Gros Bedo wrote: If you have a long running process you wish to be able to kill at a later date the normal way of doing it would be for the script itself to write it's own PID to a file that you...
2
by: vgkhle | last post by:
hi all How can diff python scripts be called from a main script which is again a .py? os.system("python external_script.py") works fine, but what if I want to handle the un handled...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.