问题
I'm reading learn python the hard way, and on chapter 15 I'm suppose to use import argv to assign variables and raw input to gain user input. The script is:
from sys import argv
script, filename, = argv
txt = open(filename)
print " Here's your file %r :" % filename
print txt.read()
print " I'll also ask you to type it again: "
file_again = raw_input ("> ")
txt_again = open (file_again)
print txt_again.read ()
After running this script I get the error, too many values to unpack.
File "ex15.py", line 3, in
script , filename = argv
Value error: too many values to unpack
回答1:
Just a couple of pointers...
from sys import argv
script, filename, = argv
Here you're importing argv to access command line parameters, and then expecting it to contain 2 arguments - script (arg 0) and filename to print (arg1). Although the trailing comma isn't syntatically incorrect, it's not required and just looks a bit odd. I nomally leave argv
inside sys
instead of pulling it into the current namespace, but that's a matter of taste - it doesn't make a real difference. I would probably throw in a bit of error handling as well:
import sys
try:
script, filename = sys.argv
except ValueError as e:
raise SystemExit('must supply single filename as argument')
txt = (filename)
print " Here's your file %r :" % filename
print txt.read()
All that txt = (name)
is doing here is making txt have the value of filename. I believe you want to be making txt
a file object, so that you can .read()
from it:
txt = open(filename)
print "Here's the file contents of:", filename
print txt.read()
print " I'll also ask you to type it again: "
file_again = raw_input ("> ")
txt_again = open (file_again)
print txt.again.read ()
You've got the open()
here, but txt.again.read()
should be txt_again.read()
else you'll get an AttributeError
- so just change that and it's fine.
Alternatively, file objects supporting seeking, so you could just rewind
the file (as you've read the file to the end, there's nothing left to read anymore), by using:
txt.seek(0)
print txt.read()
回答2:
How are you running the script?
When you say,
script, filename = argv
you are expecting two items in argv
. The first one is the script name and the second one is a filename. If you try to run the script with more than 2 arguments then you will get such an error
python myscript.py myfile.py somethingelse
If you want to pass one more parameter to the script, then you need to specify a third variable to unpack the value into. something like this
script, filename, option = argv
Also, it would help if you paste the complete traceback
回答3:
O.K. so I found my problem I was not calling my script correctly. For example my py script is ex15.py with that script it will read a text using rw input and argv variables. And the filename for that is ex15_sample. I call the script with python ex15.py ex15_sample, I was confused with my last exercise. Where I used the variables I set in argv to call the script. But all of the feed back was very help and I also applied.
回答4:
There is an extra comma
script, filename, = argv
It should be
script, filename = argv
来源:https://stackoverflow.com/questions/12757015/too-many-values-to-unpack