[Tutor] Using sys.exit()

Andreas Kostyrka andreas at kostyrka.org
Thu Nov 2 00:56:02 CET 2006


Am Mittwoch, den 01.11.2006, 15:43 -0800 schrieb Dick Moores:
> At 12:14 AM 10/31/2006, Alan Gauld wrote:
> 
> >"Dick Moores" <rdm at rcblue.com> wrote
> > > I'd like to know how to use sys.exit() to quit a program.
> > >
> >
> >I see that you already figured that out.
> >You can also quit by raising SystemExit, which is
> >what sys.exit does... but you don't need to import sys...
> 
> I'm afraid I don't know what you mean. How do I raise SystemExit, and 
> why don't I need to import sys?

raise SystemExit(2)

is equal to sys.exit(2) (actually sys.exit(2) just raises SystemExit(2))

>>> try:
...     import sys
...     sys.exit(2)
... except SystemExit, v:
...     print v 
... 
2


And you don't need to import sys, because SystemExit is a standard
exception that are builtin, available everywhere.

Andreas

> 
> > > Is there a way to use it the way I want to? Maybe with an argument?
> >
> >You can use an argument if you want to pass an error value
> >back to the OS. This is good practice if your script might be
> >used in a batch file or shell script
> 
> So what should that value be? But if answering is a lot of bother, 
> don't, because I don't (yet) use python to write either batch files 
> or shell scripts.
> 
> > > I'm writing a script, which in a couple of places I can't
> > > use "break" to quit.
> >
> >break is not intended to quit programs, break is intended
> >to quit loops. To exit a program you should use sys.exit()
> >and if its an abnormal exit provide an argument.
> 
> Of course, I've been using break to exit only when it works. Why is 
> it wrong to do so?
> 
> Thanks,
> 
> Dick Moores
> 
> 
> >Alan G.
> >
> >
> >_______________________________________________
> >Tutor maillist  -  Tutor at python.org
> >http://mail.python.org/mailman/listinfo/tutor
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://mail.python.org/pipermail/tutor/attachments/20061102/a38185aa/attachment.pgp 


More information about the Tutor mailing list