Hi,
What is the meaning of the code lines in python. How a single variable like "argv" can be assigned to multiple variables "script and filename".
Python Code:
from sys import argv
script, filename = argv
Hi,
What is the meaning of the code lines in python. How a single variable like "argv" can be assigned to multiple variables "script and filename".
Python Code:
from sys import argv
script, filename = argv
The definition of the assignment statement is click here in the docs. As you can see, the left part of it can be a sequence of targets separated by commas. In this case, the right side must be a sequence of expression matching the left side. For example, the left side can be a sequence of identifiers (names) as in
>>> u, v, w = [1, 2, 3]
or something more complicated, such as
>>> x, ((y, z), t) = [1, ([2,3], 4)]
>>> x, y, z, t
(1, 2, 3, 4)
or even, in recent versions of python:
>>> x, *foo, y = (1, 2, 3, 4, 5, 6)
>>> foo
[2, 3, 4, 5]
In your case, the program works if the script is invoked with a single argument (the filename).
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.