Q:

Comma in except clause

I have tried to search but I couldn't find anything. However I probably just didn't word it right. In the book I am reading. A Python Book by Dave Kuhlman He writes a try:except statement to catch an IOError. 
def test():
    infilename = 'nothing.txt'
    try:
        infile = open(infilename, 'r')
        for line in infile:
            print line
    except IOError, exp:
        print 'cannot open file "%s"' % infilename

My question is what is the exp after IOError. what does it do and why is that there?

A:

It provides a variable name for the exception inside the except block:
>>> try:
...     raise Exception('foo')
... except Exception, ex:
...     print ex
...     print type(ex)
...
foo
<type 'exceptions.Exception'>

I personally find the as syntax more clear:
>>> try:
...     raise Exception('foo')
... except Exception as ex:
...     print ex
...     print type(ex)
...
foo
<type 'exceptions.Exception'>

But the as syntax wasn't introduced until 2.6, according to answers in this question.

