All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Exit Python Script

Last Updated : Mar 11, 2024

How To Exit Python Script

In this article we will show you the solution of how to exit python script, the Python exit() function is used to end the currently running programme or script. It can be used to halt the program's execution at any time.

When the exit() function is used, the programme ends up exiting right away. In this case, status is an optional input that denotes the program's departure state.

An integer value called the exit status represents the cause of programme termination.

According to tradition, a status of 0 denotes a successful execution, while any status other than zero denotes an error or an unusual termination.

"Before exit" will be printed by the programme, however if the exit() method is invoked with a status of 1, the programme will end right away without running any more code.

The line "After exit" will not print as a result. We will now discuss the idea of how to exit a python script in python with an example.

Step By Step Guide On How To Exit Python Script :-

import sys
def main():
    try:
        print("Welcome to the program!")
        # Check for termination condition
        user_input = input("Do you want to exit the program? (y/n): ")
        if user_input.lower() == "y":
            exit_program()
        # Continue with other operations
    except Exception as e:
        print(f"An error occurred: {e}")
        exit_program()
def exit_program():
    print("Exiting the program...")
    sys.exit(0)
if __name__ == "__main__":
    main()
  1. The sys module, which gives access to the exit() method, is imported at the beginning of the script.
  2. The program's entry point is the main() function. Within this method, you can add your own code and operations.
  3. There are numerous operations you can carry out inside the main() method. In this illustration, you only print a welcome message and ask the user to exit.
  4. After receiving user input, you compare it to "y" (case-insensitively) to see if the user wants to exit. You use the exit_program() function to end the script if the condition is true.
  5. A message marking the program's termination is printed by the exit_program() function, which subsequently uses sys.exit(0) to end the program. Sys.exit()'s parameter of 0 indicates that the shutdown was successful. If necessary, you can select an alternative exit code.
  6. Finally, you use the __name__ variable to determine whether the script is running as the main module. If so, you start the program by calling the main() function.

Conclusion :-

As a result, we have successfully learned how to exit a python script in python with an example.

In conclusion, this article demonstrated how to stop a Python program from running by using the exit() function.

An optional input that provides further details about the termination cause can be given as an exit status code.

You can efficiently use Python's sys.exit() function to terminate a program when appropriate by following these best practices.

I hope this article on how to exit python script helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪