0

So I've been trying out some things in Python to refresh my mind.

I've been following 'Learn Python the Hard Way' book and they introduced me to argv.

However, for whatever reason, argv seems to be taking the filename as an argument as well, which I don't think should be happening.

My code looks like this:

from sys import argv

value1, value2 = argv

print("Hi")
print("you seem bored")
print(value1)
print(value2)

And this is what I'm getting on the terminal:

userblahblah:Python nut$ python3 another_test.py hi po
Traceback (most recent call last):
  File "another_test.py", line 3, in <module>
value1, value2 = argv
ValueError: too many values to unpack (expected 2)

When I feed in one argument, this is the output:

notrealname:Python nut$ python3 another_test.py hi
Hi
you seem bored
another_test.py
hi
  • 1
    Python is built on C, so that duplicate is wildly relevant to your question. Also, close that book and burn it... – cs95 Jan 19 '18 at 12:27
  • Relevant python duplicate question: https://stackoverflow.com/questions/5222408/python-sys-argv0-meaning-in-official-documentation – Juan Jan 19 '18 at 12:28

1 Answers1

2

The argv array contains the name of your python file at the first (0th) position. If you want to access the arguments, you need to start at index 1.

ignored, value1, value2 = argv
C-Otto
  • 5,615
  • 3
  • 29
  • 62