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.

agf
  • 171,228
  • 44
  • 289
  • 238
RanRag
  • 48,359
  • 38
  • 114
  • 167
  • actually the error is pointing to api i am using but if u want error is ` File "series.py", line 19, in 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' – RanRag Sep 13 '11 at 00:19

2 Answers2

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.

agf
  • 171,228
  • 44
  • 289
  • 238
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.

agf
  • 171,228
  • 44
  • 289
  • 238
Seth
  • 45,033
  • 10
  • 85
  • 120
  • 1
    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. – agf Sep 13 '11 at 01:21