3

I am trying execute my Python script as:

python series.py supernatural 4 6
Supernatural : TV Series name 
4 : season number
6 : episode number

Now in my script I am using the above three arguments to fetch the title of the episode:

import tvrage.api
import sys

a =  sys.argv[1] 
b = sys.argv[2]
c =  sys.argv[3]

temp = tvrage.api.Show(a)
name  = temp.season(b).episode(c)  # Line:19
print ( name.title)

But I am getting this error:

File "series.py", line 19, in <module>:
  name = super.season(b).episode(c) 
File "C:\Python26\Lib\site-packages\tvrage\api.py", line 212, in season
  return self.episodes[n] KeyError: '4'

I am using Python 2.6.

1
  • actually the error is pointing to api i am using but if u want error is ` File "series.py", line 19, in <module> name = super.season(b).episode(c) File "C:\Python26\Lib\site-packages\tvrage\api.py", line 212, in season` return self.episodes[n] KeyError: '4' Commented Sep 13, 2011 at 0:19

2 Answers 2

3

The Python TVRage API is expecting integers, not strings (which is what you get from argv):

name = temp.season(int(b)).episode(int(c))

will correct the error, if season 4 episode 6 exists.

You should take a look at the command line parsing modules that come with Python. For 3.2 / 2.7 or newer, use argparse. For older versions, use optparse. If you already know C's getopt, use getopt.

Sign up to request clarification or add additional context in comments.

Comments

3

A KeyError means you're trying to access an item in a dictionary that doesn't exist. This code will generate the error, because there's no 'three' key in the dictionary:

>>> d = dict(one=1, two=2)
>>> d
{'two': 2, 'one': 1}
>>> d['three']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'three'

See the Python Wiki entry on KeyErrors.

1 Comment

That's a fine description of a KeyError but doesn't answer the question -- it doesn't tell him why that key doesn't exist in the dictionary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.