Exercise 16 help - not enough values to unpack

When I run the Ex 16 in the command line, I get the below error:

Traceback (most recent call last):
  File "ex16.py", line 3, in <module>
    script, filename = argv
ValueError: not enough values to unpack (expected 2, got 1)

Will someone please help me figure out what’s happening here and how to solve it? I’ve checked the first few lines of code, even copy/pasted them from the book to be sure I wasn’t missing some dumb error. Still doesn’t run. Googled a couple forum posts (stackoverflow, etc), but they only confused me.


from sys import argv

script, filename = argv

print(f"We're going to erase {filename}.")
print("If you don't want that, hit CTRL-C (^C).")
print("If you do want that, hit RETURN.")


print("Opening the file...")
target = open(filename, 'W')

print("Truncating the file. Goodbye!")

print("Now I'm going to ask you for three lines")

line1 = input("line 1: ")
line2 = input("line 2: ")
line3 = input("line 3: ")

print("I'm going to write these to the file.")


print("And finally, we close it.")

And just like that, I fixed my own error. Forgot to put the “test.txt” into the command line.

Leaving this post up in case someone else has the same dumb error. ha! Cheers!

If you encase your code in the following tags, its prints much nicer on the forum…

# some code in here

For example:

from sys import argv
script, filename = argv

print(f"We’re going to erase {filename}.")
print(“If you don’t want that, hit CTRL-C (^C).”)
print(“If you do want that, hit RETURN.”)


print(“Opening the file…”)
target = open(filename, ‘W’)

Glad you cracked the bug. Its often the way when you write it out.

Hi Nashville,

Just reading your thread here. I have faced the same error but with ex17, 20, & 23. Adding “test.txt” to the command line worked for ex16.py but not the rest. Can you offer any advice? Yes, stackoverflow has only further confused me.


from sys import argv

script, filename= argv, (“open {filename}.”,“If you don’t want to wait, hit CTRL_C(^C).”,“If you want that,hit RETURN.”)

print (f"We’re going to open {filename}.")
print(“If you don’t want to wait, hit CTRL_C(^C).”)
print(“If you want that,hit RETURN.”)

txt = open(“ex15_sample.txt”,“r”)
#print(“here’s your file %r:” %filename)
print (txt.read())

print(“Type the filename again:”)
file_again=input("> ")

print (txt_again.read())