473,788 Members | 2,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sys.exit()

In a code such as:

if len(sys.argv) < 2:
print "I need arguments!"
sys.exit(1)

Is sys.exit() really a good choice? Is there something more elegant? (I
tried return but it is valid only in a function)
--
--
Every sufficiently advanced magic is indistinguishab le from technology
- Arthur C Anticlarke
Jul 18 '05 #1
12 88321
"Ivan Voras" <iv****@fer.h r> wrote in news:bm******** **@bagan.srce.h r:
In a code such as:

if len(sys.argv) < 2:
print "I need arguments!"
sys.exit(1)

Is sys.exit() really a good choice? Is there something more elegant? (I
tried return but it is valid only in a function)


More elegant might be to put your code in a function which would let you
use return, although even then you need to call sys.exit somewhere if you
want to set a return code. Remember that sys.exit just throws an exception,
so you can always catch it further out if you need to. Also you can combine
the print into the call to sys.exit and save a line:

import sys

def main(args):
if len(args) < 2:
sys.exit("I need arguments!")

print "rest of program..."

if __name__=='__ma in__':
main(sys.argv)

--
Duncan Booth du****@rcp.co.u k
int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
"\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?
Jul 18 '05 #2
Ivan Voras wrote:

In a code such as:

if len(sys.argv) < 2:
print "I need arguments!"
sys.exit(1)

Is sys.exit() really a good choice? Is there something more elegant? (I
tried return but it is valid only in a function)


sys.exit() is the proper, defined, cross-platform way to exit from
a program and return a value to the calling program. Change your
definition of elegant and you could consider it easily the most elegant
of all solutions. ;-)

-Peter
Jul 18 '05 #3
Peter Hansen wrote:
Ivan Voras wrote:
Is sys.exit() really a good choice? Is there something more elegant?
(I tried return but it is valid only in a function)
sys.exit() is the proper, defined, cross-platform way to exit from
a program and return a value to the calling program. Change your
definition of elegant and you could consider it easily the most
elegant of all solutions. ;-)


Ok. :)

(Just for the record: I was looking for something that doesn't require a
module import. But it is not important.)

--
--
Every sufficiently advanced magic is indistinguishab le from technology
- Arthur C Anticlarke
Jul 18 '05 #4
Ivan Voras wrote:
In a code such as:

if len(sys.argv) < 2:
print "I need arguments!"
sys.exit(1)

Is sys.exit() really a good choice? Is there something more elegant? (I
tried return but it is valid only in a function)


An alternative that I often choose is:

raise SystemExit("I need arguments!")

This is the same in one line, and I think it is more elegant, because it
is higher-level: you are not using the low-level interface of error codes,
a non-programmer may not understand what '1' means, usually it means success
so that can be very confusing. I prefer raise SystemExit.

Gerrit.

--
30. If a chieftain or a man leave his house, garden, and field and
hires it out, and some one else takes possession of his house, garden, and
field and uses it for three years: if the first owner return and claims
his house, garden, and field, it shall not be given to him, but he who has
taken possession of it and used it shall continue to use it.
-- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
http://people.nl.linux.org/~gerrit/
Kom in verzet tegen dit kabinet:
http://www.sp.nl/

Jul 18 '05 #5
Gerrit Holl wrote:
An alternative that I often choose is:

raise SystemExit("I need arguments!")

This is the same in one line, and I think it is more elegant, because
it
is higher-level: you are not using the low-level interface of error


Yes, I agree. This is what I was looking for (as always, it was obvious
:) ), thanks. Only, what error code is returned for this termination method?

--
--
Every sufficiently advanced magic is indistinguishab le from technology
- Arthur C Anticlarke
Jul 18 '05 #6
Ivan Voras wrote:
Gerrit Holl wrote:
An alternative that I often choose is:

raise SystemExit("I need arguments!")

This is the same in one line, and I think it is more elegant, because
it
is higher-level: you are not using the low-level interface of error


Yes, I agree. This is what I was looking for (as always, it was obvious
:) ), thanks. Only, what error code is returned for this termination method?


For a string, I believe it is 1, although I don't know when this holds and
when it doesn't - I don't care for myself, so I never tried to find out, really ;)

Gerrit.

--
258. If any one hire an ox-driver, he shall pay him six gur of corn per
year.
-- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
http://people.nl.linux.org/~gerrit/
Kom in verzet tegen dit kabinet:
http://www.sp.nl/

Jul 18 '05 #7
On Thu, 09 Oct 2003 20:44:55 +0200, Gerrit Holl wrote:
Ivan Voras wrote:
Gerrit Holl wrote:
> An alternative that I often choose is:
>
> raise SystemExit("I need arguments!")
>
> This is the same in one line, and I think it is more elegant, because
> it
> is higher-level: you are not using the low-level interface of error


Yes, I agree. This is what I was looking for (as always, it was obvious
:) ), thanks. Only, what error code is returned for this termination method?


For a string, I believe it is 1, although I don't know when this holds and
when it doesn't - I don't care for myself, so I never tried to find out, really ;)


Actually, SystemExit is a *lower* level operation. From the docs:
[ http://www.python.org/doc/current/lib/module-sys.html ]
=============== =============== =============== =============== ====
exit( [arg])

Exit from Python. This is implemented by raising the SystemExit
exception, so cleanup actions specified by finally clauses of try
statements are honored, and it is possible to intercept the exit
attempt at an outer level. The optional argument arg can be an integer
giving the exit status (defaulting to zero), or another type of
object. If it is an integer, zero is considered ``successful
termination'' and any nonzero value is considered ``abnormal
termination'' by shells and the like. Most systems require it to be in
the range 0-127, and produce undefined results otherwise. Some systems
have a convention for assigning specific meanings to specific exit
codes, but these are generally underdeveloped; Unix programs generally
use 2 for command line syntax errors and 1 for all other kind of
errors. If another type of object is passed, None is equivalent to
passing zero, and any other object is printed to sys.stderr and
results in an exit code of 1. In particular, sys.exit("some error
message") is a quick way to exit a program when an error occurs.
=============== =============== =============== =============== ====

So SystemExit is called by sys.exit. And one can use:

sys.exit('I need arguments!')

Thus it would seem that sys.exit is higher level, and probably a bit more
stable and portable.
-- George

Jul 18 '05 #8
George Young wrote:
So SystemExit is called by sys.exit. And one can use:

sys.exit('I need arguments!')

Thus it would seem that sys.exit is higher level, and probably a bit more
stable and portable.


Ah, yes. I was probably confused with another function, I think with
os._exit. I don't really understand why _exit is in the os module
while exit is in the sys module; I have grown accostumed to raising
SystemExit, but I can de-grow it again I guess ;)

Gerrit.

--
240. If a merchantman run against a ferryboat, and wreck it, the master
of the ship that was wrecked shall seek justice before God; the master of
the merchantman, which wrecked the ferryboat, must compensate the owner
for the boat and all that he ruined.
-- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
http://people.nl.linux.org/~gerrit/
Kom in verzet tegen dit kabinet:
http://www.sp.nl/

Jul 18 '05 #9
BDFL has said: http://www.artima.com/weblogs/viewpost.jsp?thread=4829

"Ivan Voras" <iv****@fer.h r> wrote in message
news:bm******** **@bagan.srce.h r...
In a code such as:

if len(sys.argv) < 2:
print "I need arguments!"
sys.exit(1)

Is sys.exit() really a good choice? Is there something more elegant? (I
tried return but it is valid only in a function)
--
--
Every sufficiently advanced magic is indistinguishab le from technology
- Arthur C Anticlarke
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
8527
by: Guinness Mann | last post by:
Pardon me if this is not the optimum newsgroup for this post, but it's the only .NET newsgroup I read and I'm certain someone here can help me. I have a C# program that checks for an error condition and if it finds it it notifies the user with a MessageBox and then on the next line of code (example in a minute) it calls Application.Exit(). To my astonishment, I stepped through the code with the debugger, and watched it call...
1
17714
by: Brendan Miller | last post by:
I am trying to close my application using Application.exit() in the frmMain_Closing event. When the form closes the process does not. My application only has one form (no other classes either). The form has a really long loop which generates approx. 700 .csv files. I do not create any threads myself (Application.exitthread() doesn't work either). To counteract this I have decided to use the Environment.exit(-1) method instead. What...
4
10861
by: Bob Day | last post by:
Using VS 2003, VB.net... I am confused about the Application.Exit method, where the help states "This method does not force the application to exit." Aside from the naming confusion, how do I force the application to exit? See **** below for my comments/questions in a simple example. ****In Sub Main, the Application.Exit works as expected, other sub main code is ignored, and when the end sub is reached the application shuts down....
1
5117
by: =?Utf-8?B?VGFvZ2U=?= | last post by:
Hi All, When I use applcation.exit() in winForm application, the form closed, but the process is still going!! ( The debug process is still running if debug in VS IDE). Environment.Exit(0) works fine. But how to do in such following scenario: if I need to give 2 option for user,1. Quit 2. Restart. In Quit option, I use Environment.Exit(0) to confirm the process will be stopped. In Restart option, I can't use Environment.Exit(0) , because...
16
3614
by: Laurent Deniau | last post by:
I would like to know if the use of the pointer ref in the function cleanup() below is valid or if something in the norm prevents this kind of cross-reference during exit(). I haven't seen anything in the norm against this, I mean an as-if rule saying "atexit registered functions are executed as-if they were called from main", making val out of scope at this point. a+, ld. #include <stdio.h>
11
2461
by: yawnmoth | last post by:
To quote from <http://php.net/function.include>, "Because include() is a special language construct, parentheses are not needed around its argument. Take care when comparing return value." exit is a language construct, also. To quote from <http://php.net/ function.exit>, "this is a language construct"
11
2291
by: =?Utf-8?B?U3RldmVEQjE=?= | last post by:
Hi all. I'm using VS 2008 Express C++. I created a console application back in 1999, and updated it for VC++ 6.0 in 2001. I've updated again this past month, and have found enough differences that I now need to use a different exit routine. In 2001, I could use a basic escape with a do {}while(ch!= 27); Now that apparently does not work. So, how do I exit a function now with C++ 2008 express?
39
2826
by: mathieu | last post by:
Hi there, I am trying to reuse a piece of code that was designed as an application. The code is covered with 'exit' calls. I would like to reuse it as a library. For that I renamed the 'main' function into 'mymain', but I am stuck as to what I should do for the 'exit'. AFAIK there is no portable way to catch the exit. The only thing I can think of is atexit, but that does not work since 'exit' is still called afterward.
0
2111
by: Gary Robinson | last post by:
In Python 2.5.2, I notice that, in the interpreter or in a script, I can exit with: exit() But I don't see exit() mentioned as a built-in function; rather the Python Library Reference says we should use sys.exit(). Also, the reference says sys.exit() is like raising SystemExit. But so is just calling exit(). For instance, exit('some error message') has the same apparent effect as
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10177
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5402
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2896
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.