38

I've got a SyntaxError on my except:

try:
    opts, args = getopt.getopt(sys.argv[1:], 'P:D:H:d:u:p:nvhmJi:c:Ml:TB:',
            ['host=', 'port=', 'directory=', 'user=', 'password=',
             'daemon=', 'noauth', 'help', 'verbose', 'mysql', 
             'icounter=', 'config=', 'nolock', 'nomime', 'loglevel', 'noiter',
             'baseurl='])
except getopt.GetoptError, e:
    print usage
    print '>>>> ERROR: %s' % str(e)
    sys.exit(2)

I get the error:

File "main.py", line 199

except getopt.GetoptError, e:

SyntaxError: invalid syntax

Anyone have any idea?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
lagarkane
  • 915
  • 2
  • 9
  • 22

2 Answers2

59

You use python3 and in python3 the raise syntax no longer accepts comma-separated arguments.

Use as instead:

except getopt.GetoptError as e:

This form is also backwards-compatible with 2.6 and 2.7.

georg
  • 211,518
  • 52
  • 313
  • 390
  • I'd add that if the OP wants code that's compatible with python<2.6 then he should use `except getopt.GetoptError:` and then retrieve the error information using `sys.exc_info()` – Bakuriu Feb 16 '13 at 10:48
9

Your syntax is invalid for catching the exception

You should have written except getopt.GetoptError as e: instead of except getopt.GetoptError, e:

Abhijit
  • 62,056
  • 18
  • 131
  • 204