Using arguments in python - value error - python

I am new to python programming and I tried this code:
from sys import argv
script, first, second, third = argv
print "this script is called:" ,script
print "the first variable is called : " ,first
print "the second variable is called : ", second
print "the third variable is called : " ,third
I am getting the error :
Traceback (most recent call last):
File "/Users/richavarma/Documents/first.py", line 4, in <module>
script, first, second, third = argv
ValueError: need more than 1 value to unpack
My output should be as follows:
this script is called: abc.py
the first variable is called: first
the second variable is called : second
the third variable is called : third

In short, argv takes arguments from the command line. If you type this command into the command line:
python test.py first second third
You will pass 4 arguments to your python code: test.py, first, second, and third
You can take in all 4 arguments as inputs by assignment like so:
from sys import argv
(filename, arg1, arg2, arg3) = argv
After this you can use any of the arguments as strings with their variable names.

Related

I dont know exactly what a argv does and I need a explaination

So I'm reading a book and I saw that he did this:
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)
and it give this error but in the book it works:
Traceback (most recent call last):
File "c:\Users\FORCE 5 CORE\Desktop\visual studio code file\1\ex1.py", line 123, in <module>
script, first, second, third = argv
ValueError: not enough values to unpack (expected 4, got 1)
The script is trying to get 4 arguments passed to it, but you didn't pass any, so, argv only has the script name, because it is provided by your OS (hence, "expected 4, got 1")
You need to run the script with 3 more arguments, for example:
python ex1.py val1 val2 val3
It is the Command Line Argument for Python. You pass these values with while calling the program.
It is basically an Array.
script, first, second, third = argv
When you call your function from terminal with these arguments script, first, second, third = argv like:
>>python program_name.py 1 2 3
You are basically assigning script as program_name.py , first as 1, second as 2 , and third as 3.
To avoid getting the error, you have to provide all arguments while running the program from terminal.

I was testing argv import in python and there is an error

I was reading "Learn Python 3 the Hard Way" and was testing the 'from sys import argv' there is an error occuring here is the sample code of the book:
from sys import argv
first, second, third = argv
print("The scipt is called:", script)
print("Your first variable is:", first)
print("Your second variable is:,", second)
print("Your third variable is:", third)
An I got this error:
Traceback (most recent call last):
File "d:/MyCodes/PythonCodes/jon.py", line 2, in
first, second, third = argv
ValueError: not enough values to unpack (expected 3, got 1)
Argv will be an array with your commandline arguments this is trying to split the list into, first, second, third.
Use argn to know how many arguments to expect
from sys import argn, argv
for i in range(argn):
print(f”arg {i}: {argv[i]}”)
argv returns list of prarameter passed in command line
argv[0] = script name
argv[1] = first value passed in to program, and so on.
from sys import argv
script,first, second, third = argv
print("The scipt is called:", script)
print("Your first variable is:", first)
print("Your second variable is:,", second)
print("Your third variable is:", third)
For this script your command should look like this
python filename.py firstval secondval thirdval
for Variable lengthn arguments you can use argn as mentioned in one of the answer
from sys import argv
# read the WYSS section for how to run this
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)
In WWSS section(What You Should See) in first line you see what you should enter in
PowerShell. Line goes like this:
$ python3.6 ex13.py first 2nd 3rd
First thing:
You wrote your code wrong on line two: look mine to see where you have mistaken,
You are missing script variable.
Second:
What you probably entered in terminal is this:
$ python3.6 ex13.py
Notice how you should enter 3 variables that are missing.
Argv are named before running the script in the same line as the script name.
Hope this helped you!

Increasing understanding of Python argument variable

For the following Python script named ex13.py:
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 have a question about the following line of code:
script, first, second, third = argv
We are saying assign argv to four variables on the left in this order.
Then when I, use this script in my terminal:
python ex13.py first 2nd 3rd
I understand that we are passing variables to a script using the terminal as an input method. However what's confusing me is this.
When I was writing basic Python scripts the way I would call them is with:
python ex3.py
Am I correct in saying that this python ex3.py is not passing a single command line argument and python ex13.py first 2nd 3rd is passing several?
When working from the command line, argv returns a list of the command line arguments, where the first (at place zero argv[0] we get the python file name that was used after the pyhton name).
from place one and up, the values are related to the order in which arguments was recieved. take note that if you use optional arguments (python myscript.py -p 127.0.0.1) those are counted in argv too. so you will get argv[1] == -p
Am I correct in saying that this python ex3.py is not passing a single
command line argument and python ex13.py first 2nd 3rd is passing
several?
no, you are incorrect, python ex3.py is passing 1 argument, argv[0] = ex3.py.

Printing individual script arguments repeatedly prints all of them

I'm reading "Learn Python the Hard Way" and I'm on exercise 13, but I'm running into a little trouble. Whenever I open up the command line and run the script and enter my argument variables, it's always duplicated on every line like this:
C:\users\blah\> program.py first second third
The script is called:['program.py','first','second','third']
Your first variable is:['program.py','first','second','third']
Your second variable is:['program.py','first','second','third']
Your third variable is:['program.py','first','second','third']
So instead of seeing this on every line I'm aiming for something that doesn't duplicate every line like this (without brackets):
C:\users\blah\> program.py first second third
The script is called:'program.py'
Your first variable is:'first'
Your second variable is:'second'
Your third variable is:'third'
This is my original code. Note I'm on Python 3.5, even though it's specified to use 2.7.
from sys import argv
script = argv
first = argv
second = argv
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)
Pretty sure the whole variable part is off.
The code in the example is:
from sys import argv
script, first, second, third = argv
When you execute your script with program.py first second third argv becomes a list containing the name of your script followed by each of the scripts arguments. So argv becomes this:
argv = ['program.py', 'first', 'second', 'third']
The line in the original code 'unpacks' the members of that list and assigns them to individual variables. It is equivalent to:
script = argv[0]
first = argv[1]
second = argv[2]
third = argv[3]
It is simply a more concise way of expressing the above. This unpacking syntax is one of the things Zed Shaw is trying to teach with that example. When you changed the program to:
script = argv
first = argv
second = argv
third = argv
the 'unpacking' went away - each of your variables simply refers to the whole list, as you can see from the output of your modified program.
You have not posted the code but this is a very simple issue. When you provide command line arguments python takes it as a list. So when I say
import sys
print sys.argv
Then from the command line i type:
python program.py first second third
the output is what you are getting i.e. all the variables as a list.In order to get the words seperately use list indices. So to get the first argument you use sys.argv[0] which would give program.py
So your code would be like this:
import sys
print 'script name:', sys.argv[0]
print '1st var:', sys.argv[1]
#and so on

Python argv not working

I'm new to python and i'm just reading practicing different things and i'm trying to figure out why argv isn't working for me
from sys import argv
script, bike, car, bus = argv
print ("The script is called:"), script
print ("The first variable is:"), bike
print ("The second variable is "), car
print ("Your third variable is : "),bus
I'm getting an error of need more than 1 value to unpack
Traceback (most recent call last):
File "ex13.py", line 6, in <module>
script, bike, car, bus = argv
ValueError: need more than 1 value to unpack
I am running my example program from the command-line by calling:
python ex13.py
Your example is better written as (to cope with arbitrary usages):
from sys import argv
script, args = argv[0], argv[1:]
print("The script is called: ", script)
for i, arg in enumerate(args):
print("Arg {0:d}: {1:s}".format(i, arg))
The reason you'd be getting an error (place show Traceback) is because you're calling your script with fewer arguments than you are trying to "unpack".
See: Python Packing and Unpacking and Tuples and Sequences where it says:
This is called, appropriately enough, sequence unpacking and works for
any sequence on the right-hand side. Sequence unpacking requires the
list of variables on the left to have the same number of elements as
the length of the sequence. Note that multiple assignment is really
just a combination of tuple packing and sequence unpacking.
To demonstrate what's going on with your example adn eht error you get back:
>>> argv = ["./foo.py", "1"]
>>> script, a = argv
>>> script
'./foo.py'
>>> a
'1'
>>> script, a, b = argv
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack
The error should be obvious here; You are trying to unpack more values than you have!
To fix your example I'd do this:
from sys import argv
if len(argv) < 4:
print ("usage: {0:s} <bike> <car> <bus>".format(script))
raise SystemExit(-1)
script, bike, car, bus = argv
print ("The script is called:", script)
print ("The first variable is:", bike)
print ("The second variable is ", car)
print ("Your third variable is : ", bus)
Update: I just noticed this; but all your print()(s) are wrong. You need to either use str.format() or put the argument inside the print() function.
Pycharm is an ide and i just click run and it runs the program, but in powershell i type in python ex13.py and that runs the program
OK, then you aren't passing any arguments. So, what were you expecting to find as as the first, second, and third arguments? PowerShell isn't going to guess what bike, car, and bus you wanted to pass the program, any more than it's going to go out and buy you a bike, car, and bus. So, if you want to run this program with arguments representing your bike, car, and bus, you have to actually do that:
python ex13.py CR325 Elise VW
Then your script will output those arguments.
Well, actually, it may not, because your print calls are wrong. If this is Python 2.7, those parentheses don't do anything, so you'll see:
The script is called: ex13.py
The first variable is: CR325
The second variable is Elise
The third variable is : VW
If it's Python 3.x, the parentheses wrap the arguments to the print, just like any other function, so the , script and so forth aren't part of the print, so you'll just see:
The script is called:
The first variable is:
The second variable is
The third variable is :

Categories