How to terminate a python script

0 votes
In PHP there is a die() function that is used to display a message and exit the script. Is there something similar in Python?
Aug 9, 2019 in Python by Neel
• 3,020 points
22,574 views

1 answer to this question.

0 votes

This can be done by importing the sys module.

import sys
sys.exit()

As per the documentation,

sys.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 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.

Since exit() ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.

Hope it works!!

If you are a beginner and need to know more about Python, It's recommended to go for Python Certification course today.

Thanks!

answered Aug 9, 2019 by Arvind
• 3,040 points

Related Questions In Python

0 votes
5 answers

how to exit a python script in an if statement

Instead of using the normal UTF-8 encoding, ...READ MORE

answered Jul 4, 2023 in Python by bodymist
• 140 points
349,179 views
+1 vote
1 answer

How to Profile a script in Python

Python includes a profiler called cProfile. It ...READ MORE

answered Nov 21, 2018 in Python by SDeb
• 13,300 points
1,423 views
0 votes
1 answer

How to write a python script for downloading a video from YouTube and saving it to a directory?

First download pytube using the following code  pip ...READ MORE

answered Sep 4, 2019 in Python by Neel
• 3,020 points
2,413 views
0 votes
1 answer

How to execute a Python script from the Django shell?

Hello @kartik, The << part is wrong, use < instead: $ ./manage.py shell ...READ MORE

answered Jun 22, 2020 in Python by Niroj
• 82,880 points
9,805 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,051 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
3,468 views
+1 vote
1 answer

How to install a module in Python 3.6 ?

If you are using Ubuntu then the ...READ MORE

answered Jun 25, 2019 in Python by Arvind
• 3,040 points
962 views
+1 vote
1 answer

How to convert records from csv to a list in Python?

If you are using Python 3.x then ...READ MORE

answered Jun 25, 2019 in Python by Arvind
• 3,040 points

edited Jun 26, 2019 by Kalgi 9,543 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP