Content · Programming · Python

Python Exit Codes for Successful and Failed Operations

Use exit codes so scripts know whether a command succeeded.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Use exit codes so scripts know whether a command succeeded.

What It Does and Why It Matters


Most command-line conventions use status 0 for success and a nonzero code for failure. Raising SystemExit or returning a value from main and passing it to sys.exit makes this status visible to a shell. Printing an error message alone does not change the exit status.

In a real program, use raise SystemExit(main(...)); this demonstration prints codes without terminating the tutorial.

Step-by-Step Example


Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.

def main(filename):
    if not filename.endswith(".csv"):
        return 2
    return 0
print(main("table.csv"))
print(main("table.txt"))

How the Code Works


  • The definition of main creates reusable behavior; its body runs only when called or instantiated as appropriate.
  • The print calls display the results. Compare these results with the method’s documented return type and the input used.

Expected output:

0
2

Each displayed line comes from the corresponding calculation or check. The output helps verify the return values before you adapt the example to your own input.

What to Notice


Pay attention to the accepted input types, the returned value and the boundary behavior. Predict what changes if you replace the example input with an empty, zero, negative or missing value, where that value is meaningful for this operation.

If the task uses a library function, inspect its documented exceptions and return type before reusing it inside a larger function.

Where You Would Use This


This is useful for scripts, maintenance commands and automated workflows. Treat inputs as untrusted, keep normal results separate from errors, and choose exit codes consistently. Keep reusable logic outside the command-line parser so it can be tested independently.

Practice Exercise


Create a command that returns status 2 for invalid arguments and 1 for an I/O failure.

Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.

Reference: official Python documentation for the complete API contract and version notes.

Common Beginner Mistakes


  • Printing "Error" while exiting with status zero, causing callers to mistake failure for success.
  • Overlooking the documented condition: printing an error message alone does not change the exit status.

Key Takeaways


  • Use exit codes so scripts know whether a command succeeded.
  • Most command-line conventions use status 0 for success and a nonzero code for failure.
  • Create a command that returns status 2 for invalid arguments and 1 for an I/O failure.
Share this resourceSend this page to someone who may find it useful.