from sys import argv - what is the function of “script”

流过昼夜 提交于 2019-12-18 11:48:17

问题


I am reading "Learn Python the Hard Way" and was confused by the "script" part of the second line.

from sys import argv
script, filename = argv

From what I understand, the second line says: script and filename comprise argv. I tried running my code without the "script" part and it worked just fine. I'm not sure what the purpose of it is.


回答1:


Generally, the first argument to a command-line executable is the script name, and the rest are the expected arguments.

Here, argv is a list that is expected to contain two values: the script name and an argument. Using Python's unpacking notation, you can write

script = argv[0]
filename = argv[1]

as

script, filename = argv

while also throwing errors if there are an unexpected number of arguments (like one or three). This can be a good idea, depending on one's code, because it also ensures that there are no unexpected arguments.

However, the following code will not result in filename actually containing the filename:

filename = argv

This is because filename is now the argument list. To illustrate:

script, filename = argv
print("Script:", script)  # Prints script name
print("Filename:", filename)  # Prints the first argument

filename = argv
print("Filname:", filename)  # Prints something like ["my-script.py", "my-file.txt"]



回答2:


Others have explained what is script, but the python statement is called unpacking and is usually applied to tuples or sequences.

It is a shortcut way of assigning a variable for each value that is in the tuple (or sequence) to the right of the = sign.

It is not something specific to argv:

>>> a,b = ('Hello','World')
>>> a
'Hello'
>>> b
'World'

One thing to keep in mind is that the number of variables on the left side must match the number of items in the sequence on the right, else you get:

>>> a,b,c = ('Hello','World')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack
>>> a,b = ('Hello','World','!')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack



回答3:


argv is a list of the arguments to your program. Standard shell behavior includes the name of the program itself as the first argument in argv.

Python can assign multiple values at once if the number of variables on the left hand side equals the size of the list on the right hand side (it can also handle more cases, but that is the most basic). E.g.

script, filename = argv

is the same as

script = argv[0]
filename = argv[1]

Note also that that script will raise a ValueError if argv does not have exactly two elements.




回答4:


The first item in argv is the name of the Python script you're running. Any additional arguments (the filename, in this case) are arguments passed to this script.

These two arguments are assigned the names script and filename. It's entirely possible that script is never used again; it's basically a placeholder. If you remove it, however, you'd do filename = argv[1] instead.



来源:https://stackoverflow.com/questions/13666346/from-sys-import-argv-what-is-the-function-of-script

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