sys.exit()

Duncan Booth duncan at NOSPAMrcp.co.uk
Thu Oct 9 09:32:27 EDT 2003


"Ivan Voras" <ivoras at fer.hr> wrote in news:bm3msp$fjo$1 at bagan.srce.hr:

> 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__=='__main__':
   main(sys.argv)

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list