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:02 The next Pythonic tip I want to cover
0:04 is about sending an exit code to indicate
0:06 whether your application succeeded or failed.
0:09 Let's have a look, so here I have a little utility app,
0:11 mentored on Windows and its job is to format the main drive C colon
0:15 and of course you don't want to just do that,
0:18 just because the app was run, the script was run,
0:21 you'd like a little confirmation, so here we ask the user
0:23 are you sure you want to format your main hard drive?
0:25 Yes or no, you can see the default is no
0:28 but if for some reason they type in yes,
0:31 and they come down here we are going to simulate a little work,
0:34 we are not actually going to format anyone's hard drive,
0:37 and then we'll say format completed.
0:39 So let's run this.
0:42 Are you sure you want to format drive C, let's say no, format canceled,
0:46 now notice, exit code zero.
0:49 Let's come down here and say yes, I would love to format drive C,
0:53 so we do a little bit of work, you can't format it immediately it's hard job here,
0:58 and then boom, formatted successfully, enjoy the new hard drive space.
1:01 Now, we also got code zero,
1:04 if I was trying to run this as a subprocess,
1:06 or I was trying to orchestrate this by chaining it together,
1:09 there is no way for me to know as a user of this script
1:13 whether or not the user canceled or they actually formatted the hard drive.
1:17 We can easily come up here before I return we don't technically need to return,
1:22 because this is going to actually stop executing immediately;
1:26 anyway, I can go over here and I can import sys,
1:29 now I was already importing sys so that I could use this little flash command
1:32 that normally I wouldn't have been there,
1:35 so I'll say sys.exit and we are going to exit here with let's say 1,
1:39 down here, we can do this, we can say sys.exit 0 of course,
1:46 you saw if we don't do anything at all exit 0 is what is going to happen,
1:49 so maybe I'll leave this one off.
1:52 Now, we also know that this is going to throw an exception
1:54 so this returns actually unreachable PyCharm told us that,
1:58 ok so let's try again;
2:00 are you sure you want to format your main hard drive?
2:02 No. Exit code 1.
2:05 Do you want to format your hard drive- oh please do, exit code 0.
2:09 Again, canceled, exit code 1, perfect.
2:14 So if there is any chance that your script is going to be called
2:17 by other script or other applications
2:19 you want to make sure that you indicate some kind of exit code
2:22 so that they can use that information to determine
2:25 whether or not they can continue whatever they are doing afterwards,
2:27 whether or not your script succeeded.
2:30 So if we just let our app our script exit, well it's always code zero,
2:34 but we can use sys.exit and give some kind of code,
2:38 typically the convention is if it's non zero
2:41 there was some kind of either failure or abnormal exit
2:44 whereas zero is all systems nominal.
2:48 Everything is good.