在 Python 中使用命令行参数:了解 sys.argv
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4765085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using command line arguments in Python: Understanding sys.argv
提问by Zack Shapiro
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 The Hard Way。我认为这个例子可能已经过时了,所以我想在这里得到反馈。
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?
知道出了什么问题吗?
采纳答案by Ignacio Vazquez-Abrams
回答by moinudin
sys.argis 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.pyin this case). Due to your usage of unpacking, you will get a ValueErrorwhenever you pass fewer or more than 3 parameters. You can check the number before unpacking using len(argv)-1and presenting a suitable error if not 3.
请注意,第一个参数始终是脚本的名称(python script.py在本例中)。由于您使用了解包,ValueError当您传递少于或多于 3 个参数时,您将得到一个。您可以在开箱前检查编号使用len(argv)-1,如果不是 3,则显示合适的错误。
On a related note, look at getoptif you need to do more complex argument passing.
在相关说明中,查看getopt是否需要进行更复杂的参数传递。
回答by Fred
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.,
根据您有多少变量 = to argv,这是您需要减去第一个参数(脚本)的数量。例如,
script, first, second, third = argv
should have 3 arguments.
应该有3个参数。
回答by Michael
You're trying to unpack the argvinto 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()
回答by Lucky
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”可以替换为任意三个数字或单词。
回答by user3786772
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
回答by Edjarpe
Execute the code like this:
像这样执行代码:
python ch13.py first second third
回答by Sayali Sonawane
回答by beat7Bx
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 argvarguments in braces. But now it works.
我正在使用 Python 3.6.0。在我没有将argv参数包装在大括号中之前。但现在它起作用了。
回答by Jose Manuel Vazquez Castro
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


