In this video, Kathryn introduces you to the system module, and shows how to use it to read a user's command line arguments. Learn how to access all the arguments in a given list, and take apart that list to get the arguments you really want.
- Using the system module, we are going to read in a user's command-line arguments. What are command-line arguments? Well, they're the input that the user gives, via the command line, before the program's execution. In our example, the user will input numbers in the command line, and we will output the sum of these numbers. Now, there're two ways we can do this. The first is using the actual command line that comes with your Mac or PC. In the command line, all we have to do is write python to the run the file, and then the file name, which here is 03, there we go, and then we write whatever command line arguments we want to add, and so this is the input from the user, and so I could go zero, one, two, and those are my arguments.
When I hit enter, nothing is going to happen, because we haven't written anything on our program yet, but this is how you would add command line arguments. If you have an ide that you're using, usually you can go into the configurations and add the command line arguments that way. So here, if we go to edit configurations, we can go ahead and add our parameters here, and we're going to have them be the same as they were in our command line, but strings, and so we'll have zero, one, two, and the reason we have to have these quotes around them here in r configurations is because anything that's inputted into the command line is automatically a string.
Click okay, and here we are. Now, in order to get access to these command line arguments, we have to use the system module, and to use it we have to import it, and so we'll go import sys to get access to that, and to get access to our arguments, we go sys.argv. Now, if we want to print out the length of our arguments, or how many arguments we have, we just add our len function that we learned before. And so, to put this in a pretty print statement, we can go print, number of arguments.
We'll have lensys.argv, so it's going to give us how many arguments we have, and then we'll go ahead and do arguments to end that, so it'll say we have four, three, or how many arguments we have. Then, to print out the actual arguments themselves, we can go print, arguments, sys.argv. And so, if we run this, we see, okay, how many arguments to do we have? Four. What're those arguments? Well, we have the fact that this is the program that we are running, and so it has kind of the file path for the program, and then we have the arguments that we put into the configurations zero, one, two.
Now, if we run this in our command line, we can go ahead and do the up arrow, and then just hit enter, and now it runs our new program that we saved, and here we have the number of arguments. It's still four, it's just running it in the command line, versus our ide. Now, because we want to sum up our arguments, we really only want access to zero, one, and two, so we'd want to remove this first argument that has the file path of the file we're running. And so, to do this, we remove that first argument with sys.argv.remove, sys.argv.
And then, to get that first argument, we go zero here, because argv is a list. If under this, we go ahead and try to print out the arguments again, we'll see that that first argument is gone, and we just have zero, one, two, and that's what we have. Now that our data is in order, let's go ahead and try to sum up these arguments, and so we'll create a variable called arguments, and we'll just set that to system.argv. We'll also have the sum that's going to keep track of our running sum here, and then we're going to have a for loop, so we can go through each argument in our argv list.
So, we'll say for_arg in arguments, and what we're going to want to do for each argument in the arguments is we're going to want to convert it to a number and then add that number to the sum, and so, in order to convert something or cast something into a number, we have to use a try clause, and so we'll say try, and then in here, we'll try to convert it into a number, and, if it doesn't work, then we're going to go ahead and break out into an exception, and then we're going to just print that it's a bad input, because, if it can't be converted into a number, then we don't want it to be a part of our sum.
And so, to convert arg into a number, we'll go number, creating that variable, and then int arg, and so this is going to cast our argument into an int. We're going to save it in a variable called number, and then we're going to go ahead and add that number to the sum here. At the end of this, we are going to print out our sums, so that way we can see it in the output. In this case, it should be three, because zero plus one plus two equals three. Running this, we get three in our output.
Now, going into our configurations, we could change some of these numbers to say this is a four, this is a three, and so three plus four is seven, plus two should be nine, so, if we go, okay, run the program again, we get nine in the output. You could also go into your terminal and run this here, and so you could go instead of zero, one, two, you could have four, four, and four, and this'll be 12, which is what we get in the output. If your program depends on initial user input, this is a good way to go.
The user can put in their arguments, and, with the system module, you can read it and do what's appropriate for that given input.
- Working with logical and comparison operators
- Getting a list of numbers with the range() and list() functions
- Using mathematical functions such as round(), abs(), and pow()
- Calculating a given input's length
- Importing and using the math module
- Reading a user's command-line arguments
- Getting the current time
- Formatting dates and times with datetime
- Creating a timer
- Using urllib to get content from the Internet
- Using the JSON module to decode content