简体   繁体   中英

Exiting a python script cleanly

I am trying to exit a python script without the annoying error message coming up:

Traceback (most recent call last):
  File "<pyshell#27>", line 3, in <module>
    sys.exit()
SystemExit

I have tried quite a few things, but none have worked. Here's an example:

while True:
    print "hi", #this just tests to see if I have exited.
    try:
        sys.exit()
    except SystemExit:
        print "Exited"

NB: The solution doesn't have to be anywhere close to this code, that was just an example of something I have tried

When I run this in the Python console (under Windows or Linux) I don't get any sort of error message when I use sys.exit(-1) . However, iPython will give me

An exception has occurred, use %tb to see the full traceback.

SystemExit: -1

I suspect you are seeing error messages because you are working in some sort of IDE, is that the case?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM