How to terminate a Python script

前端 未结 10 1216
予麋鹿
予麋鹿 2020-11-22 04:34

I am aware of the die() command in PHP which exits a script early.

How can I do this in Python?

10条回答
  •  滥情空心
    2020-11-22 05:03

    While you should generally prefer sys.exit because it is more "friendly" to other code, all it actually does is raise an exception.

    If you are sure that you need to exit a process immediately, and you might be inside of some exception handler which would catch SystemExit, there is another function - os._exit - which terminates immediately at the C level and does not perform any of the normal tear-down of the interpreter; for example, hooks registered with the "atexit" module are not executed.

提交回复
热议问题