Home Blog 3 Ways to End a Program in Python

3 Ways to End a Program in Python

by Nagoor Vali

Fast Hyperlinks

  • Which Methodology Ought to You Use to Terminate a Program in Python?

Figuring out find out how to finish a program is necessary and may be helpful in numerous situations. For example, for those who’re making a easy sport, you’ll be able to give up on particular person enter or when a sure situation is met. Uncover the variations between the 3 ways of stopping a Python program.

1. Utilizing give up() or exit()

One of many easiest methods of ending a Python program is to make use of both of the built-in strategies, give up() or exit(). Once you name both give up() or exit(), this system will terminate.

The give up() and exit() features do precisely the identical factor, it’s only a matter of non-public desire which you employ.

This is how you should use the give up() operate to finish a Python program:

        for num in vary(10):
    if num == 9:
        give up()
    print(num)

The pattern program above will print integers from 0 to eight on the console. As soon as num is 9, this system exits. It’s also possible to use the give up() command to exit the Python Built-in Growth and Studying Setting (IDLE), which lets you run Python scripts interactively.

Notice that each give up() and exit() depend on the web site module so that you shouldn’t use them in manufacturing environments. The following methodology, sys.exit(), is a greater possibility.

2. Utilizing sys.exit()

Once you name sys.exit() in your program, Python raises a SystemExit exception. It accepts an elective argument to specify an integer exit code (0 by default). It’s also possible to go an object which is able to end in an exit code of 1 and can print the article’s string worth to straightforward error.

        import sys 

for num in vary(10):
    if num == 5:
        sys.exit("You exited this system")
    print(num)

The output of this system will likely be as follows:


Python code output screenshot

Just like the give up and exit strategies, sys.exit() additionally raises a SystemExit exception. This implies which you could name sys.exit() or elevate SystemExit(). Each settle for an elective argument.

You must study crucial Python features for those who do not perceive how the code above works.

3. Utilizing os._exit()

The third manner of exiting a program is the particular os._exit() methodology. You possibly can go it an elective argument to specify an exit standing code. os._exit() comes from the os module and terminates the method instantly with out performing the traditional cleanup actions that happen when a Python program exits.

As a result of this operate exits with out performing regular cleanup, you must solely use it in particular cases. Based on the Python documentation, a very good instance is in a baby course of after a fork (a brand new course of created utilizing os.fork()).

This is an instance of find out how to use the os._exit() operate to finish a program:

        import os
import sys

def child_process():
    print("Youngster course of is working.")
    os._exit(0)

def essential():
    print("Mother or father course of is working.")

    attempt:
        pid = os.fork()

        if pid == 0:
            
            child_process()
        else:
            
            print(f"Mother or father course of is continuous with youngster course of PID: {pid}")
    besides OSError as e:
        print(f"Fork failed: {e}")
        sys.exit(1)

    print("Mother or father course of is finished.")

if __name__ == "__main__":
    essential()

The code above will output the next:


Python code output screenshot

Which Methodology Ought to You Use to Terminate a Program in Python?

Python offers numerous strategies to finish a program. Nonetheless, most of those strategies do the identical factor. This could cut back the burden of decision-making if it’s essential add an exit mechanism to your program when studying Python.

In abstract, sys.exit() ought to be your go-to methodology for ending a program in Python. It raises a SystemExit exception with an elective exit code.

The give up() and exit() strategies are extra acceptable when utilizing the Python REPL. You must keep away from them in manufacturing as a result of they rely on the web site module, which can not at all times be accessible. You must hardly ever want to make use of os._exit(), except you’re working with code that forks processes.

Source link

Related Articles

Leave a Comment

Omtogel DewaTogel