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:
Answer from lvc on Stack Overflowany other object is printed to stderr and results in an exit code of 1
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
There are 3 approaches, the first as lvc mentioned is using sys.exit
sys.exit('My error message')
The second way is using print, print can write almost anything including an error message
print >>sys.stderr, "fatal error" # Python 2.x
print("fatal error", file=sys.stderr) # Python 3.x
The third way is to rise an exception which I don't like because it can be try-catch
raise SystemExit('error in code want to exit')
it can be ignored like this
try:
raise SystemExit('error in code want to exit')
except:
print("program is still open")
You're looking for calls to sys.exit(...) (exit(...) calls sys.exit(...)) in the script. The argument to that method is returned to the environment as the exit code.
It's fairly likely that the script is never calling the exit(...) method, and that 0 is the default exit code.
From the documentation for sys.exit:
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.
One example where exit codes are used are in shell scripts. In Bash you can check the special variable $? for the last exit status:
me@mini:~$ python -c ""; echo $?
0
me@mini:~$ python -c "import sys; sys.exit(0)"; echo $?
0
me@mini:~$ python -c "import sys; sys.exit(43)"; echo $?
43
Personally I try to use the exit codes I find in /usr/include/asm-generic/errno.h (on a Linux system), but I don't know if this is the right thing to do.
Return vs sys.exit()
Is sys.exit() bad practice?
Python logging sys.exit('message') - Stack Overflow
Is it better to quit a script due to a user input error using sys.exit(1) or raise Error()?
Videos
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?