Using command line arguments in Python: understanding sys.argv

I am currently experiencing Learn Python The Hard Way. I think this example may be missing, so I wanted to get feedback here.

I am using Python 3.1

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 get this error:

Traceback (most recent call last):
  File "/path/ch13.py", line 3, in <module>
    script, first, second, third, bacon = argv
ValueError: need more than 1 value to unpack

Any idea what is wrong?

+5
source share
9 answers

You forgot to pass script arguments, for example. foo.py bar baz quux.

enter image description here

+8
source

To pass arguments, you need to run the script as follows:

python fileName.py argument1 argument2

Depending on how many variables you have = up argv, this is how much you need to have minus the first argument (script). For instance.

 script, first, second, third = argv

3 .

+4

sys.arg - . script, . IDE, :

python script.py first second third

, script (python script.py ). - ValueError , . len(argv)-1 , 3.

getopt, .

+3

argv . , . :

a, b, c = [1, 2, 3]

, :

a, b, c, d, e = [1]

, . sys.argv , , , , , script. :

if len(argv) == 5:
    script_name, a, b, c, d = argv
else:
    print "This script needs exactly four arguments, aborting"
    exit()
0

, , script. , python (then your filename.py) 1 2 3. "1, 2 3" .

0

script , . - :

python /path/ch13.py first second third
0

:

python ch13.py first second third
0
from sys import argv
a, b, c, d = argv
print "The script is called:", a
print "Your first variable is:", b
print "Your second variable is:", c
print "Your third variable is:", d

script : s.py

script : enter image description here

0

(script, first, second, third) = argv 

3

python filename arg1 arg2 arg3

.

Python 3.6.0. argv . .

you can check it out here

0

All Articles