Using command line arguments in Python: Understanding sys.argv

送分小仙女□ 提交于 2019-12-19 07:31:10

问题


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?


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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()



回答5:


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.




回答6:


Execute the code like this:

python ch13.py first second third



回答7:


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



回答8:


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:




回答9:


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.




回答10:


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


来源:https://stackoverflow.com/questions/4765085/using-command-line-arguments-in-python-understanding-sys-argv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!