在Python中使用命令行参数:了解sys.argv

时间:2023-02-01 23:16:35

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.

我目前正在学习Python的艰难之路。我想这个例子可能已经过时了,所以我想在这里得到反馈。

I'm using Python 3.1

我正在使用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?

知道什么是错的吗?

10 个解决方案

#1


8  

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

你忘了将参数传递给脚本,例如foo.py bar baz quux。

在Python中使用命令行参数:了解sys.argv

#2


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.,

根据您拥有的变量数= argv,这是您需要减去第一个参数(脚本)的数量。例如。,

 script, first, second, third = argv

should have 3 arguments.

应该有3个参数。

#3


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:

sys.arg是命令行参数列表。您需要实际将命令行参数传递给脚本以填充此列表。在IDE的项目设置中执行此操作,或者在命令行上执行此操作:

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.

请注意,第一个参数始终是脚本的名称(在本例中为python script.py)。由于您使用了解包,每当传递少于或多于3个参数时,您将获得ValueError。您可以在使用len(argv)-1解压缩之前检查数字,如果不是3,则显示合适的错误。

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

在相关的说明中,如果您需要执行更复杂的参数传递,请查看getopt。

#4


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:

您正在尝试将argv解压缩为单独的值。解包需要确切的值与要解压缩的值的大小相匹配。考虑一下:

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:

会给你一个丑陋的错误,你刚才制作的。以您的方式解压缩sys.argv特别糟糕,因为它是用户输入,而您不知道脚本用户将提供多少个参数。所以你应该仔细打开包装:

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

#5


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.

您只需在打开脚本时键入任意三项内容即可。例如,运行python(然后是你的filename.py)1 2 3.“1,2和3”可以用任意三个数字或单词替换。

#6


0  

Execute the code like this:

像这样执行代码:

python ch13.py first second third

#7


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

#8


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

将此脚本另存为:s.py

Run this script from terminal as follows: 在Python中使用命令行参数:了解sys.argv

从终端运行此脚本,如下所示:

#9


0  

You can do

你可以做

(script, first, second, third) = argv 

and pass 3 arguments

并传递3个参数

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.

我使用的是Python 3.6.0。在我没有将argv参数包装在大括号中之前。但现在它有效。

在Python中使用命令行参数:了解sys.argv

#10


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

我想这个例子对你有帮助。您可以传递所需的参数数量,即参数数量是可变的。 :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

#1


8  

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

你忘了将参数传递给脚本,例如foo.py bar baz quux。

在Python中使用命令行参数:了解sys.argv

#2


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.,

根据您拥有的变量数= argv,这是您需要减去第一个参数(脚本)的数量。例如。,

 script, first, second, third = argv

should have 3 arguments.

应该有3个参数。

#3


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:

sys.arg是命令行参数列表。您需要实际将命令行参数传递给脚本以填充此列表。在IDE的项目设置中执行此操作,或者在命令行上执行此操作:

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.

请注意,第一个参数始终是脚本的名称(在本例中为python script.py)。由于您使用了解包,每当传递少于或多于3个参数时,您将获得ValueError。您可以在使用len(argv)-1解压缩之前检查数字,如果不是3,则显示合适的错误。

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

在相关的说明中,如果您需要执行更复杂的参数传递,请查看getopt。

#4


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:

您正在尝试将argv解压缩为单独的值。解包需要确切的值与要解压缩的值的大小相匹配。考虑一下:

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:

会给你一个丑陋的错误,你刚才制作的。以您的方式解压缩sys.argv特别糟糕,因为它是用户输入,而您不知道脚本用户将提供多少个参数。所以你应该仔细打开包装:

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

#5


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.

您只需在打开脚本时键入任意三项内容即可。例如,运行python(然后是你的filename.py)1 2 3.“1,2和3”可以用任意三个数字或单词替换。

#6


0  

Execute the code like this:

像这样执行代码:

python ch13.py first second third

#7


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

#8


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

将此脚本另存为:s.py

Run this script from terminal as follows: 在Python中使用命令行参数:了解sys.argv

从终端运行此脚本,如下所示:

#9


0  

You can do

你可以做

(script, first, second, third) = argv 

and pass 3 arguments

并传递3个参数

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.

我使用的是Python 3.6.0。在我没有将argv参数包装在大括号中之前。但现在它有效。

在Python中使用命令行参数:了解sys.argv

#10


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

我想这个例子对你有帮助。您可以传递所需的参数数量,即参数数量是可变的。 :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