Python [sys] 05 Exit Codes

how to end a program in python This is a topic that many people are looking for. is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, would like to introduce to you Python [sys] 05 Exit Codes. Following along are instructions in the video below:
Whats up guys ritalin. Oh. Theyre coming back at you with another batch batch to tour it whoa whoa thats not it this isnt a bachelor.
This is a python tutorial and i thought personally that i should take the extra measure the extra step the extra nine yards to make sure that that got into your head. Thats the only reason. I made that mistake right there not because im an idiot.
But only because i want you to know that this is a python tutorial not batch. Okay all right were working with the sis module. Today and weve been working with it with a past couple of videos.
But this time. Were gonna work with something new and this is kind of like a simple kind of a strange idea to talk about but its actually a little sort of like convention or standard for exiting your program. I know thats thats its weird it sounds kind of silly.
But it when you whenever your program exits from the command line or from any other process. So that sort of thing whenever something ends on your computer. It has whats called an exit code and that exit code actually determines or like tells the system or even tell you as the developer or the the power user whatever youd like to be called how the program actually ran and now did it run successfully did it do what it need to do or did it have an error did something go wrong.
And thats why the program had to close so thats these are these are called exit codes and we actually can manipulate those whenever. We exit python or actually use a system module that sort of thing. So there is of course with the search malt.
The sis module a way to exit your program and just like there isnt typical python and were actually gonna see this more and more even in the next upcoming module that were gonna be looking at in python. But they always have this optional parameter as to whether or not we want an exit code. So just for some little experiment just to show you guys whats going on how were gonna be doing this im actually gonna break out of the python shell in this video and go into the bash shell or like the linux command proper the shell so im gonna drag this over you guys can see it and what im gonna do is go ahead and write echo.
Now echo is the program. If you know batch or any other shell this should be pretty easy echo is just gonna give some senate output. So im just say pretty simple whats up youtube now there we go now bash.
This has this nice variable thatll let us know what the exit code of the previous command was its actually a dollar sign question mark and that is zero the last command echo. The last program that we ran had an exit code of zero now zero is typically success it means that it ran it ran well there was no problems. It was nothing wrong with it it successfully ran now if i ran echo.
Whats up youtube. But i forgot the h maybe it says a coup its gonna be like what what that what are you talking about now if we actually check out what the the variable is that the exit code of the last command itll give us 127. Now thats an error code.
Thats something something was wrong something did not go well. It did not end in a zero. And we have a problem here anything anything not zero is typically something wrong now these two typically have a sort of like a specific convention of what the number actually means and were gonna go into that more in a later module.

how to end a program in python-0
how to end a program in python-0

But anything other than zero is bad so uh thats what were gonna be working with in python because weve got this this module and we can work something here. So. What im gonna do is go ahead and create a new little script for us.
Very quickly and very easily. Ill just write a shebang line. Because you know its been so long since ive actually recorded myself making some cool stuff and im sure you guys remember the syntax.
The way that i typically operate if name is equal to main now i have my little commented curly brackets the sort of thing ill save this as just file dot python to make it nostalgic and ill run our main function well we can go ahead and declare that or define. It with the df keyword right up here. Now i typically would be using object oriented just for good practice so you guys know what to do but were gonna want anyway to import our sis module.
So what theres this module. What we can do just make this pretty easy. Im gonna go ahead and pray hello.
There and im gonna have it do something strange hmm. What i want to do actually can i do import string and i want to get some fancy things in here. If i did import string.
I want to actually have it ask for your name. And if there are any numbers in your name. Then itll throw an error okay.
So what we can do hmm. What i want to do here. Yeah.
We can loop through this and actually make something interesting so lets say name equals raw input. This is some good review for you guys. What is your name and what we can do is we can start a for loop.
Actually now we can test whether or not there anything in there so four digits in string dot digits. So four digit. It should be probably thats a better idea we can go ahead and test.
If see digit is in the name now if it is what we want to do is go ahead and exit cuz thats bad. It wouldnt typically have a number in your name. So we can run sis dot exit.
Now you can see theres an exit status or an exit code here and were gonna say like plain old how about that anything other than zero. Thats gonna let us as the programmer or the user. Know that there was a problem.

how to end a program in python-1
how to end a program in python-1

If not what we can do is do print. Oh. Its good to meet you were gonna add on the name their little string concatenation and then we can do assists exit.
Just to be safe and so we as programmers know what were thinking here. We can exit with the zero exit code or status. So thats pretty pretty simple and pretty easy now.
If i actually had a terminal open and try to run this ill get everything idle away. Here. Ill make this nice.
Now. If i ran python file. Hi.
There whats your name. John oh. Its good to meet you john now if i were to check out what the exit code was with our little bash syntax here thats zero because nothing went wrong.
It the program ran perfectly fine. But if we were to run it one more time and i said john 3. 312.
Its gonna have a problem. Its not gonna know what to do so lets actually check out what the exit code was now and in our case. Its 2 just like we supplied inside of our actually as an argument for our cyst on exit command or function.
So thats all that i wanted to get across to you guys all that all that really were going to be addressing in this tutorial. Is that there are these strange things called exit codes or exit. Statuses that determine whether or not your program ran successfully or not and with the sis module.
We can manipulate that we can do that in python as well even without the sis module. But the sis module just gives us another way or another option to allow us to do that in fact if we were to remove sis as the preceding object itll still run this perfectly fine seriously far right john 32. Its not gonna its not gonna know what to do.
But we still have to there because the actual built in function for python actually has support for this exact same sort of syntax or mindset. So we can use an exit code as an argument for exit command whether or not using the sis model or even just a built in python function.
So there you go guys hopefully. That was pretty easy pretty simple to understand and something that you can learn from anyway. I hope you guys loyd this ill see you in the next tutorial and i dont really have anything witty or funny to say to you for the end of this video.

how to end a program in python-2
how to end a program in python-2

Thank you for watching all the articles on the topic Python [sys] 05 Exit Codes. All shares of are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.

Leave a Reply

Your email address will not be published. Required fields are marked *