One little tool that can prove to be very useful in some circumstances is arg v from within the SIS module. Basically what sis dot arg V is, is a list of command line arguments passed to a Python script, where arg V of zero is the script name itself. Now, please note that it is operating system dependent, whether this is a full path name or not, we will see this very soon for the windows 10 operating system. So, let's create a Python script and use sis dot arg V and then pass some arguments to the Python command when running it from the windows command line. I have created this script right here on my D drive called my script dot p y. So first of all, let's see the contents of the SIS dot arg v list.
For this. I'm going to import the SIS module and then I'm going to just Print out, sis dot arg v. Let's save the script as it is, and run it in the windows command line. So by Thawne, the my script.py, enter. So the result is a list with a single element, the full path name of the file. In order to extract this element as a string. Let's use index zero inside the script and then run the file once again.
So let me edit the script sis dot arg V of zero, save. And now let's run the file once again. Okay, so we got the file name out of the SIS dot arg v list. Great. Now what if we want to pass multiple arguments to the Python command, and then have these arguments passed further into the script? Well, in this case, the first argument of the SIS dot arg v list will still be the name of the file and all the subsequent arguments will be the a D arguments that we add to the Python command in the windows cmd.
So let's try this. Let's iterate over the SIS dot arg v list. And for the first element of the list, the one at index zero, let's print out the string. This is the file name, followed by the actual name of the file. So without further ado, let me write this. So let's say for argument in sis dot arg V, colon, and I said that if sis dot arg v dot index of argument, so if the index of the argument is equal to zero, meaning it is the first element in the list, then let's say print, this is the file name plus argument.
Otherwise, for each of the subsequent arguments that we're going to pass to the Python script, let's print out a slightly different string. So that would be the else clause. Else print. Let's say this is an argument, plus argument. Okay? Now let's save the file and run the script once again, also adding some arguments.
So we have Python, the name of the script. And let's add three more arguments. Let's say hi, hello, and greetings. I know it's kind of a silly example, but I want you to understand the main idea behind sis.rv. That's what's important at this point. So let's hit Enter.
And notice the results. We still got the first element of the CS dot arg v list printed out to the screen. Actually, let me delete this index right here. In order for us to see the entire list as it is with these additional arguments, enter. Okay, so notice the results. We still got the SIS dot arg v list printed out to the screen this time Having multiple elements, with the first one being the file name and the others being the arguments that we just passed to the script while running the Python command in the CMD.
Next we got the result of the for loop and the if else block right here, which is pretty straight forward and doesn't need any additional explanation, I think. Now let's upgrade this script. And let's say that we want to skip the file name from the SIS dot arg v path. And we also want to print out the index the position of each argument in the SIS dot arg v list, if you remember from earlier in the course, in order to iterate over a sequence by index and by element at the same time, we can use the enumerate function. So that would mean let me delete this first. So for index and argument in enumerate of CS dot arg v star at index one, colon, and now let's say print argument, and the space plus STr of index plus the colon, plus argument.
So notice that I used a slice inside the parentheses of the enumerate function sis dot arg V of one colon. Because I said we want to skip the file name, which is positioned at index zero. Next inside the print function right here, for each element in the SIS dot arg v slice, we are concatenating, the word argument with the arguments index converted to a string with a colon. And finally with the argument itself. Now let's save the file and run it once again in the command line, also passing the same three arguments as earlier. Okay, this time, the result is much more prettier and informative, I think.
So this is the way in which you can use C dot arg V in order to pass command line arguments to your Python script. I hope this video has been useful and I will see you very soon in the next one. Bye