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.
I'm using 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?
To use, sys. argv in a Python script, we need to impo r t the sys module into the script. Like we import all modules, "import sys" does the job. Ideally, we want this at the top of the script, but anywhere before we use the sys.
The second parameter, argv (argument vector), is an array of pointers to arrays of character objects. The array objects are null-terminated strings, representing the arguments that were entered on the command line when the program was started.
Execute the Python code in command. command can be one or more statements separated by newlines, with significant leading whitespace as in normal module code. If this option is given, the first element of sys. argv will be "-c" and the current directory will be added to the start of sys.
You forgot to pass arguments to the script, e.g. foo.py bar baz quux
.
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.,
script, first, second, third = argv
should have 3 arguments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With