Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

from sys import argv - what is the function of "script"

Tags:

python

argv

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.

like image 456
user1869775 Avatar asked Dec 02 '12 04:12

user1869775


People also ask

What does from sys import argv do?

Short answer import the argv list from the sys package. argv contains parameters passed in to python on the command line.

What is import argv in Python?

argv is a list containing the arguments passed to the python interpreter when called from a command line.

How does Sys argv work in Python?

sys. argv is the list of commandline arguments passed to the Python program. argv represents all the items that come along via the command line input, it's basically an array holding the command line arguments of our program. Don't forget that the counting starts at zero (0) not one (1).

What is argv used for?

The first element of the array, argv[0] , is a pointer to the character array that contains the program name or invocation name of the program that is being run from the command line. argv[1] indicates the first argument passed to the program, argv[2] the second argument, and so on.


2 Answers

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"]
like image 112
Waleed Khan Avatar answered Sep 22 '22 11:09

Waleed Khan


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
like image 35
Burhan Khalid Avatar answered Sep 20 '22 11:09

Burhan Khalid