简体   繁体   中英

Python sys.exit with a number

if __name__ = '__main__':
    blah = False
    if something:
        blah = True

    if blah:
        sys.exit(1)            <--- is this necessary?

would program still exit with 1 if blah is False?

If blah is False then the sys.exit(1) line would not be executed.

Provided there is no other code following the code you posted, Python will exit normally, which means the exit code will be 0 , the same as if sys.exit() had been called without an argument.

Exit code meanings are a convention; 0 means success, anything else usually means there was a problem. Explicitly exiting the program with sys.exit(1) simply means the program adheres to that convention; it is signalling something to whatever started the script, namely that things didn't work out.

Some programs use exit codes for more than just failure communication; exit codes can be used to communicate a range of states, but that is entirely up to the program. Check the documentation to be sure what status codes mean, status codes are not really standardized, it is all more of a... guideline.

TLDR: the default exit code for most programs is 0 , meaning succes, and Python is no exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM