0

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?

2
  • 3
    you are correct. keep in mind though that argv[0] will contain the name of the script regardless of how many parameters you passed it Commented Jan 17, 2019 at 11:36
  • @Nullman he is NOT correct - as you mention youself, python ex3.py is passing the argument 'ex3.py'. Commented Jan 17, 2019 at 12:20

1 Answer 1

1

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 and python ex13.py first 2nd 3rd is passing several?

no, you are incorrect, python ex3.py is passing 1 argument, argv[0] = ex3.py.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.