Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Foundational Concepts
Lecture: Care enough to send an exit code

Login or purchase this course to watch this video and the rest of the course contents.
0:01 The next Pythonic tip I want to cover is about sending an exit code to indicate whether your application succeeded or failed.
0:10 Let's have a look, so here I have a little utility app, meant to run on Windows and its job is to format the main drive C:
0:16 and of course you don't want to just do that, just because the app was run, the script was run,
0:22 you'd like a little confirmation, so here we ask the user "are you sure you want to format your main hard drive?"
0:26 "Yes" or "no", you can see the default is "no" but if for some reason they type in "yes",
0:32 and they come down here, we are going to simulate a little work, we are not actually going to format anyone's hard drive,
0:38 and then we'll say "format completed". So let's run this. "Are you sure you want to format drive C?" Let's say "no", format canceled,
0:47 now notice, exit code zero. Let's come down here and say "yes", I would love to format drive C,
0:54 so we do a little bit of work, you can't format it immediately, it's hard job here,
0:59 and then boom, formatted successfully, enjoy the new hard drive space. Now, we also got code zero, if I was trying to run this as a subprocess,
1:07 or I was trying to orchestrate this by chaining it together, there is no way for me to know as a user of this script
1:14 whether or not the user canceled or they actually formatted the hard drive.
1:18 We can easily come up here before a return, we don't technically need to return, because this is going to actually stop executing immediately;
1:27 anyway, I can go over here and I can import "sys", now I was already importing "sys" so that I could use this little flush command
1:33 that normally I wouldn't have been there, so I'll say "sys.exit" and we are going to exit here with let's say 1,
1:40 down here, we can do this, we can say "sys.exit(0)" of course, you saw if we don't do anything at all, "exit(0)" is what is going to happen,
1:50 so maybe I'll leave this one off. Now, we also know that this is going to throw an exception
1:55 so this "return" is actually unreachable, PyCharm told us that, OK so let's try again; "are you sure you want to format your main hard drive?"
2:03 No. Exit code 1. Do you want to format your hard drive - oh please do, exit code 0. Again, canceled, exit code 1, perfect.
2:15 So if there is any chance that your script is going to be called by other script or other applications
2:20 you want to make sure that you indicate some kind of exit code so that they can use that information to determine
2:26 whether or not they can continue whatever they are doing afterwards, whether or not your script succeeded.
2:31 So if we just let our app, our script exit, well it's always code zero, but we can use "sys.exit" and give some kind of code,
2:39 typically the convention is if it's non-zero there was some kind of either failure or abnormal exit whereas zero is "all systems nominal".
2:49 Everything is good.

Talk Python's Mastodon Michael Kennedy's Mastodon