[no audio]
Friends, here we are going to discuss about one of the operation from your
Sys Module, that is called 'sys.argv', 'sys.arguments'
Now what is this? See 'sys.argv' returns a list that consists
of command line arguments which are passed to your Python
script. Simply it is giving a list means it is a variable
now. Actually it is one of the operation from your Sys Module.
We know operation may be a function or maybe a variable.
So 'sys.argv' is giving a list.
Right? So which consists of some command line arguments, which are
passed to your Python script.
So to understand this, first of all you have to know what
is a command line argument, right?
Let me open some script.
Let me open my editor.
So I am going to write a script called
[no audio]
'command_line_arguments', 'command_line_arguments.py', so
something. I'm writing
nothing is there. Simply I am writing, "hello".
[no audio]
That's it.
Now, I am going to run from command line.
So if you want to work, if you want to know command line arguments,
always you have to run your script from command line only. Fine.
See just now we have written a script called, this one.
Nothing is there simply, print('hello').
Let me run this 'python command_line_arguments',
your script name, '.py'
Yes, you're getting some output. Right. Now while running your
script, while running your script by your Python interpreter,
I am also passing some values called, suppose, "hi how are
you", some 1 3 67,
whatever it may be. See if I pass in this way just observe that do you
have any disturbance with your script?
Is there any wrong output? No, it's working perfectly.
See after your script name if I pass some values
they are not disturbing your script.
First of all in this way
if you pass, yes, actually that is the correct. You can pass in that way.
There is no wrong in that. If it is wrong, then you have to
get some error, right?
You are not getting any error.
That means yes, you can run your script by passing some values
in this way. Now after your script name whatever you're passing,
right, so these all are called command line arguments for
your script. While running your script you're passing some
values. These are called command line arguments, right?
So if you are passing some command line arguments here, if
you're passing some command line arguments here how you can
get them inside of your Python script?
[no audio]
Right. See nothing
is there. What I am doing is simply, now let me remove 'pring('hello')'.
You can write anything, right?
I am writing 'print(sys.argv)', that is a variable but
you are using 'sys', right?
That's why what you have to do? You need to 'import sys'.
[no audio]
Right? Now see. First of all observe your script, what you have inside
of your script. Nothing is there. I am using 'sys'.
That's why I am importing that 'sys' first. Then I am printing
simply 'sys.argv'.
First I am running your script in this way, guys. Observe
the output what you are getting? You are getting a list which
consists of your script name only because after your script
name, you're not passing anything. In case if you pass what
will happen? Just I am passing, suppose 'python',
some 'shell', some 'perl', something like that.
So we'll see the output. Whatever you are passing here,
all these values are coming in this variable called 'sys.argv' as a list.
Not only that, you can pass some values also, see the output.
Now 'sys.argv' is capturing inside of your script
whatever the values, whatever the arguments you are passing
while running your script, right?
So these are the command line arguments you are passing while
running your script and you can handle these values inside
of your script with the help of 'sys.argv'.
It's very, very helpful.
[no audio]
Right? And you need to remember one more thing, whatever the
values you are passing from command lines even though if
it is a number see that it is treating as a string.
That's why you have a quotation here. You know in Python,
right, if I write x equals to some list consists of 4, 5
then here 4 and 5 are values. Observe the output. You are
not having any quotations. But here you having a quotation
for your numbers,
that means whatever you are passing from command line all
will treat as a string by your Python, right. Fine.
Let me take one simple example to understand this example.
Yeah, one more thing guys. See, this is going to give all your
command line arguments, right?
And if you observe your output, the very first value is always
your script. How many values you are passing?
How many command line arguments you are passing?
Right. Whatever you are passing,
the index 0 is always your script name. See that. Now I am passing
only one command line argument. You are getting index 0
as your script name, remaining all are your command line arguments,
right? So if I want to display only script name what I have
to do? 'sys.', if you remember, if you remember list operations,
I want to get only the first index value.
I mean the first value in your list, that is index is always
0, right?
Let me rerun and see the output. Because of, let me open your
script once again here, because of 'prints(sys.argv[0])' you are
getting this command line arguments as a script name.
0 is always script name in your 'sys.argv',
because 'sys.argv' is a list, in that first value is
always your script name.
That's what I am getting with index 0.
That's fine.
Now guys, I have a small requirement.
My requirement is, my script has to read some string,
my script has to read some string.
Then after that, I need to convert, based on my requirement
sometimes I will convert into lowercase,
sometimes I will convert into uppercase.
sometimes I will convert into title format.
Right. Forget about first your command line arguments.
So if you want to provide some string to your script what
you have to do?
Let me do this thing.
[no audio]
Let me take 'usr_str =', if you want to read a string
always you have to take 'input' as a, let me save it.
[no audio]
'actions_on_strings.py', right? See here.
I am reading a string from command line, 'usr_str = input',
enter your string, I am asking.
Then after that, 'usr_action', I am reading. What is that action
means, 'input("Enter your action
[no audio]
on your string", so "Valid actions are", just I am giving the
clue. "Valid actions are:" like "lower/upper/title" format.
That's it.
So these are the values I need to read from user. Now assume
that user is going to enter some string and user is going
to enter suppose lower or upper or title format, now based on
user request.
I need to print your string in lowercase or uppercase or
in the title format.
That's why here I need to take 'if' condition first of all,
right. 'if usr_action == lower',
[no audio]
right, then I have to print your given string, that is 'usr_str' into
lowercase. We have different options here,
right. User may enter, right 'upper' also. So I am checking
'if usr_action == upper',
then I am going to print my 'usr_str' in terms of uppercase.
[no audio]
Right. 'elif 'usr_action', if 'usr_action =="title"' format,
then I am printing 'usr_str.title'. In case by mistake
if user enters other than lower/upper/title,
then I'm writing 'else' block, and I am saying
"Your option is invalid".
New Text
Right, so "Please select valid options from this list".
So what are the valid options? "lower/upper/title"
That's it. Fine.
Right, let me go and run this script from command line.
So first of all your script, this is your script.
Let me run it and see the output first.
I am running. I am providing string called, some randomly
I am providing in this way, "Python scripting", something like
that. Then I want to convert my string into lower or upper
or title. Any option
you can provide. I am giving, suppose title format, then see
the output. You're getting in the title format. Then I'm rerunning
and now I am going to provide 'lower'.
I want to convert my string into lower case. Yes, fine.
[no audio]
But whatever the script you have now your script needs two
inputs - one is string, one more is user action. One is string
one more is user action. But while running script you are
providing, right.
See after running I am interacting with my script.
Yes, you can interact but my intention is I don't want to
provide in this way.
[no audio]
I have to run with one step.
I have to run my script in one step, then how you can do it?
See for that I have a idea.
What I will do is, first command line arguments
I will send as a, I will send a string, required string like
"Python scripting". Then second argument, right,
I will provide action, like lower. Then I will enter so that
it has to work.
[no audio]
Right. Now if you pass in this way you have to use your Sys Module,
and you can handle them - string as your first argument, right
and action as second argument.
Now, what I am doing is, instead of using 'input' now I am going
to comment these two things. Now, I am going to import your
Sys Module, then 'usr_str' I am taking, guys is if you
remember 'sys.argv'
is going to give your all command line arguments, right.
In that zeroth argument is the script name. Then see if you
are running in this way,
if you are running in this way, let me do that thing first, otherwise.
I will comment your entire code.
Right. Now what I am doing is I'm going to print your
'print(sys.argv)' first
Right. Now, let me run this and see the output.
If you run in this way, you're getting a list inside of your
script with the help of 'sys.argv'. In that index 0 is script
name, index 1 is your given string, and index 2 is user
action. That's what you are getting by running in this way
with the help of 'sys.argv'.
Right. So guys whenever if you want to pass an argument,
which consists of some spaces then you have to write your
entire string inside of quotation.
Right. Now see. Now because with the help of 'sys.argv' you're
able to get your command line arguments inside of your script.
That's why what I am doing is, 'usr_str' I can assign index 1, right.
So 'sys.argv' index 1 I am assigning for user string, and user action
'sys.argv' index 2. That's it.
Right. Now, let me run your script and see the magic. Now,
I'm not going to use 'input' command, and I'm not going to provide
multiple lines input for your script, only single step, just
running this way. See the output. You're getting your string
in lower case. Maybe you are getting confusion because the entire
thing is in lowercase. Let me do
[no audio]
in this way.
[no audio]
Right. See the output. You are getting string in lowercase or
let me pass here upper.
[no audio]
Let me past title.
[no audio]
Yes, it's perfectly working.
[no audio]
Right. But here a small drawback
is there. What does that mean? By mistake suppose if I'm not
passing this, you're getting an error.
"List index out of range", because you are passing only script
name and string name.
What about action? Action is always index 2 position you
have taken inside of your script, but you are not passing three
values. If you pass three values, then you can take index
3. You're passing only two values, that is script name and
then string name.
This is zeroth index.
This is first index. Then what about second index? You are
not passing, but you are sending that. You are sending that
index 2 value to this action.
So without having second index value, how can you assign
that value to other variables? That's not possible.
That's why you're getting an error.
Right? So whenever if you are using command-line arguments with
the help of 'sys.argv' you have to take care.
What is that care? How you can take care, right? Nothing
is there. Very simple. Observe that. So guys for our script
for our script as of now, whatever we are working using lower/
upper/ title format,
how many command line arguments we need? One is script name,
second one is string, third one is action.
So three values you are passing then only it is correct.
If you pass two values, then it is not correct.
Even if you pass more than three, no need of four values.
That's why what I am doing is, before running, before assigning
your command line arguments to your variable,
first of all I am finding, 'print("The number of command line arguments")'.
[no audio]
If you remember, if you have a list, if you have
a list you can find out number of values in that by using
'length' function.
So 'sys.argv' is a list, right and I'm finding number of values.
Let me comment your entire code first.
[no audio]
Now run your script and see the output.
Yes, you are getting three because you're passing script
name, first command line argument, second command line argument.
If I pass in this way, what is that?
[no audio]
Yeah, with only, suppose something
[no audio]
then you're getting two. Whenever if you get two, that is not
correct. You're not running your script in the proper way
because you need three values finally here, including
your script name - script name, your string, and then your action.
That's why what I am doing is whenever if this value is not
equal to 3, right. 'if', see that. 'if length' of your
'sys.rgv', if it is not equal to 3, if it equals 3, no problem.
If it is not equal to 3 then I can't assign second argument
value to your user action.
[no audio]
Not equal to 3 means
assume that you are sending only two values, then it's not correct
no. That's why whenever if your number of arguments are
not equals to three, maybe two or more than three,
I need only exactly three including your script name - script
name, string name, and action.
It should have three only. In case if it is not equal to 3
then I want to stop my script. You know, you can stop your
script using 'sys.exit'. Previously
we discussed this right in previous video.
[no audio]
Now, see the magic.
Now you don't get any error. Previously whenever if you don't
have this logic,
let me cut it and save it and run it
once again. You are getting an error, right? "index range"
"IndexError: list index out of range".
But what I am doing is I am saving this now with the help of this simple
'If' condition. Now, I am running. You're not getting any error.
Simply your Python is stopping your script whenever if you are not
passing proper command line arguments. But simply stopping
is not a good practice. Whenever if you're not passing exactly
three arguments then simply you are stopping, right. Before stopping
just try to write some message. 'print
"Usage of this script". "Usage:". "Usage: is, 'print' your
'sys.argv', 'argv', script name, then after that what you have to do?
Right? Let me do 'f' strings.
[no audio]
So usage I'm writing, so print your script name then, then
here you have to pass,
pass your string.
Your required string
I'm writing suppose, "your required string" here.
Then here you have to write action. Action is like lower or
[no audio]
upper or title.
That's it.
Now, I am saving this and I am going to run. Guys try to understand
this. It's very useful.
I am running in this way.
Then you are getting usage is, you have to run this script
in this way, "action_on_strings.py", then your string, then some action.
you have to provide like lower or upper or title.
Now, I am going to provide in the proper way. Suppose
I am running upper, now it's working properly.
If you are not passing command-line arguments perfectly,
then only are getting usage,
otherwise it's working perfectly, right?
So guys finally here
we got the concept of 'sys.argv',
and 'sys.exit' as well.
So that's why 'sys.exit' is, 'sys.exit'
is very helpful in your real-time. Why we are using? Just
to stop your script. When we are stopping?
If you are not passing proper command line arguments.
If you are not passing proper command line arguments what
will happen? Because of this line you are getting an error.
I don't want to get an error.
Right? That's why. When you will get an error? If you are not passing
proper arguments.
So whenever if you are not passing proper arguments, I am
going to stop my script by simply saying this information to the user.
"Please run this script in this way". Usage of script
I am giving, and then I am stopping. Then next time whenever
the user is going to run your script
he will take care how to run a script, right? Okay.
Okay guys, thank you for watching this video.
It is very, very important while writing your scripts
'sys.argv', right. Fine.
[no audio]