Reputation: 1224
For the following Python script named ex13.py:
from sys import argv
script, first, second, third = argv
print("The script is called:", script)
print("Your first variable is:", first)
print("Your second variable is:", second)
print("Your third variable is:", third)
I have a question about the following line of code:
script, first, second, third = argv
We are saying assign argv
to four variables on the left in this order.
Then when I, use this script in my terminal:
python ex13.py first 2nd 3rd
I understand that we are passing variables to a script using the terminal as an input method. However what's confusing me is this.
When I was writing basic Python scripts the way I would call them is with:
python ex3.py
Am I correct in saying that this python ex3.py
is not passing a single command line argument and python ex13.py first 2nd 3rd
is passing several?
Upvotes: 0
Views: 58
Reputation: 2208
When working from the command line, argv returns a list of the command line arguments, where the first (at place zero argv[0]
we get the python file name that was used after the pyhton
name).
from place one and up, the values are related to the order in which arguments was recieved. take note that if you use optional arguments (python myscript.py -p 127.0.0.1
) those are counted in argv too. so you will get argv[1] == -p
Am I correct in saying that this
python ex3.py
is not passing a single command line argument andpython ex13.py first 2nd 3rd
is passing several?
no, you are incorrect, python ex3.py
is passing 1 argument, argv[0] = ex3.py
.
Upvotes: 1