Using command line arguments in Python: Understanding sys.argv [duplicate]
Asked Answered
P

11

6

I'm currently going through Learn Python The Hard Way. I think this example might be out dated so I wanted to get feedback here on it.

I'm 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'm getting 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's wrong?

Prosser answered 21/1, 2011 at 23:53 Comment(0)
L
9

You forgot to pass arguments to the script, e.g. foo.py bar baz quux.

enter image description here

Lint answered 21/1, 2011 at 23:55 Comment(2)
I'm a noob, what is the correct way to do that with the code I've written?Prosser
He said you must run command line in this exercise. Open CMD (Window) and type: ex13.py script_value first_value second_value third_valueClevie
B
4

sys.arg is a list of command line parameters. You need to actually pass command line parameters to the script to populate this list. Do this either in your IDE's project settings or by running like this on the command line:

python script.py first second third

Note that the first argument is always the script's name (python script.py in this case). Due to your usage of unpacking, you will get a ValueError whenever you pass fewer or more than 3 parameters. You can check the number before unpacking using len(argv)-1 and presenting a suitable error if not 3.

On a related note, look at getopt if you need to do more complex argument passing.

Birck answered 21/1, 2011 at 23:59 Comment(0)
B
4

In order to pass arguments, you will need to run the script in this manner:

python fileName.py argument1 argument2

Depending on how many variables you have = to argv, this is how many you need to have minus the first argument (script). E.g.,

 script, first, second, third = argv

should have 3 arguments.

Bullis answered 30/11, 2011 at 5:1 Comment(0)
C
1

You can do

(script, first, second, third) = argv 

and pass 3 arguments

python filename arg1 arg2 arg3

when you run it from command line.

I am using Python 3.6.0. Before i was not wrapping the argv arguments in braces. But now it works.

you can check it here

Cenesthesia answered 31/5, 2017 at 14:40 Comment(0)
E
0

You're trying to unpack the argv into separate values. Unpacking requires, that the exact amount of values is matched by the size of the value to unpack. Consider this:

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

works fine, but this:

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

will give you the same ugly error, that you just produced. Unpacking sys.argv in the way you did is especially bad, because it's user input, and you don't know, how many arguments the users of your script will supply. So you should unpack it more carefully:

if len(argv) == 5:
    script_name, a, b, c, d = argv
else:
    print "This script needs exactly four arguments, aborting"
    exit()
Ekg answered 2/10, 2012 at 8:1 Comment(0)
D
0

All you have to do is type any three things when opening the script. For example, run python (then your filename.py) 1 2 3. The "1, 2 and 3" can be replaced with any three numbers or words.

Drusilladrusus answered 9/4, 2014 at 0:24 Comment(0)
B
0

In order to run this script on the command line, you need to use three arguments. You will have to type something similar to the following:

python /path/ch13.py first second third
Branca answered 5/9, 2014 at 1:23 Comment(0)
D
0

Execute the code like this:

python ch13.py first second third
Denysedenzil answered 4/7, 2016 at 15:11 Comment(0)
E
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

Save this script as: s.py

Run this script from terminal as follows: enter image description here

Eisenhower answered 29/7, 2016 at 10:4 Comment(0)
S
0

I think this example will help you. You can pass the number of arguments you want, that is, the number of parameters is variable. :D

def main(*args):
    print("Arguments Passed: ", args)

if __name__ == '__main__':
    name_Script, *script_args = sys.argv
    print("Name of the script: ", name_Script)
    main(*script_args) #Send list of arguments
Stilt answered 7/7, 2018 at 2:20 Comment(0)
E
0

Here is another way of writing it:

*script_args = sys.argv
Egerton answered 8/8, 2021 at 0:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.