Calling sys.exit with a string will work. The docs mention this use explicitly:

In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs.

To be more specific this will also result in an exit code of 1:

any other object is printed to stderr and results in an exit code of 1

Answer from lvc on Stack Overflow
Discussions

Difference between exit(0) and exit(1) in Python - Stack Overflow
What's the difference between exit(0) and exit(1) in Python? I tried looking around but didn't find a specific question on these lines. If it's already been answered, a link would be sufficient. More on stackoverflow.com
🌐 stackoverflow.com
Is it better to quit a script due to a user input error using sys.exit(1) or raise Error()?
I think that's just a style decision; I don't think there is a best practice for that. Personally I'd use the raise option, probably because I like to think people use my scripts in their scripts, and the raise option allows them to catch it easier. More on reddit.com
🌐 r/learnpython
6
1
December 9, 2022
Print a custom error message and gracefully exit
How can I print a custom error message and gracefully exit the Python script at the same time? More on discuss.python.org
🌐 discuss.python.org
0
0
June 5, 2023
Return vs sys.exit()
Consider a program with threads and lots of asynchronous stuff. I have a main where at the end of it somebody has written “sys.exit(0)”. And in catching exceptions at some places there’s sys.exit(1). But I want to return some data at the end of main. If I use return statement above ... More on discuss.python.org
🌐 discuss.python.org
9
0
February 5, 2024
🌐
Python Morsels
pythonmorsels.com › exiting-a-python-program
Exiting a Python program - Python Morsels
February 21, 2022 - To exit a Python program early, call sys.exit with 0 for success, 1 for error, or a string (to print an error message while exiting).
🌐
Adam Johnson
adamj.eu › tech › 2021 › 10 › 10 › the-many-ways-to-exit-in-python
The Many Ways to Exit in Python - Adam Johnson
October 10, 2021 - If you’re looking for a quick answer, you can stop reading here. Use raise SystemExit(<code>) as the obviousest way to exit from Python code and carry on with your life. ... More obscurely, we can pass SystemExit any object, in which case ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-exit-commands-quit-exit-sys-exit-and-os-_exit
Python exit commands: quit(), exit(), sys.exit() and os._exit() - GeeksforGeeks
July 12, 2025 - Example: In the given code, the sys.exit("Age less than 18") line will terminate the Python script with a message "Age less than 18" if the variable age is less than 18. If age is 18 or greater, it will print "Age is not less than 18". This code is used to exit the script with a specific message when a certain condition is met.
🌐
freeCodeCamp
freecodecamp.org › news › python-exit-how-to-use-an-exit-function-in-python-to-stop-a-program
Python Exit – How to Use an Exit Function in Python to Stop a Program
June 5, 2023 - In this example, the program will print "Before exit", but when the exit() function is called with a status of 1, the program will terminate immediately without executing the remaining code.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › is it better to quit a script due to a user input error using sys.exit(1) or raise error()?
r/learnpython on Reddit: Is it better to quit a script due to a user input error using sys.exit(1) or raise Error()?
December 9, 2022 -

Let's say you have a script that is a simple csv parser but the user enters an invalid filepath for the csv.

Is it better to exit the script with a print("wrong filepath") and then sys.exit(1) or by using raise SomeError("wrong filepath")?

From what I read sys.exit(1) raises an exception also, but it doesn't seem to print a traceback like using raise SomeError("wrong filepath") does. So you can just print the message you want the user to see and exit quietly.

If I were to distribute a script like this to public users. What's the better practice?

🌐
Python.org
discuss.python.org › python help
Print a custom error message and gracefully exit - Python Help - Discussions on Python.org
June 5, 2023 - How can I print a custom error message and gracefully exit the Python script at the same time?
🌐
Linux Hint
linuxhint.com › python-exit-codes
Python Exit Codes – Linux Hint
Python has only two standard codes, i.e., a one and a zero. The exit code of 0 means that the process has been executed and exited successfully. This means no error was encountered. On the other hand, an error code of 1 indicates that the process exited with a failure.
🌐
Python Pool
pythonpool.com › home › blog › 4 ways of exiting the program with python exit function
4 Ways of Exiting the Program with Python Exit Function - Python Pool
January 1, 2024 - Are you using python IDLE to run python? If yes, in that case, it will exit from the window. 0 · Reply · Sophia · 4 years ago · Hi, Thank you for your article which is informative and helpful. You suggested using sys. exit() in production. I ran your code of sys.exit() and I got the message: warn(“To exit: use ‘exit’, ‘quit’, or Ctrl-D.”, stacklevel=1) I used Jupyter to run the code.
🌐
Medium
medium.com › @anupkumarray › working-with-exit-codes-between-python-shell-scripts-177931204291
Working with exit codes between Python & Shell Scripts | by Anup Kumar Ray | Medium
October 17, 2021 - On executing the shell script (which calls the python code), we get standard exit code as shown below: ... Note: If you pass anything other than an integer to sys.exit() call, the passed value will be printed and system exit status will be 1 .
🌐
Notes
henryleach.com › 2025 › 02 › controlling-python-exit-codes-and-shell-scripts
Controlling Python Exit Codes and Shell Scripts - Henry Leach
February 9, 2025 - Which might be useful for kicking ... to grep messages from stdout in your shell script. You can make the return from Python something that's not an integer, like a string, but the shell will interpret that as a 1. One final warning: out of range exit codes are interpreted as modulo 256, that mean after 255, you start going round again. If you write a program that has an exit code of 256, it'll come out as 0 and be considered successful!3. ... Tested with Python 3.11 ...
🌐
Piccolo-orm
piccolo-orm.com › blog › understanding-sys-exit
Understanding sys.exit - Piccolo Blog
April 21, 2021 - >>> if python successful_script.py; then echo "successful"; else echo "error"; fi; successful >>> if python failing_script.py; then echo "successful"; else echo "error"; fi; error · Also, exit codes are very important in build tools like Docker. If the exit code indicates a command has failed, then the build will fail. If you want to indicate why the failure occured, then a string can be passed to sys.exit, in which case the exit code is treated as 1 (i.e.
🌐
Quora
quora.com › What-is-the-exit-status-code-of-a-Python-script
What is the exit status code of a Python script? - Quora
If your Python script runs successfully, it will always exit with a status code of 0. If there was an unhandled exception, it will exit with a status code of 1. If you called an exit function with a custom status code, it will exit with a status ...