Invalid Syntax error in Python Code I copied from the Internet
Question: One of my early courses in the University I attend, was some basic training in
Python 3 years ago. Now I was looking for a program that could help me resize
some Grid stuff and I found something that could help me in Python. I
reinstalled Python to my PC and found my old editor. However when I run the
code I get an invalid syntax error that I can't understand. This is the part
of the code that the error appears in :

    
    
    def downsize(mode, cell_size, inpath, outpath):
    
      from VolumeData import fileformats
      try:
        grid_data = fileformats.open_file(inpath)
      except fileformats.Uknown_File_Type, e:
        sys.stderr.write(str(e))
        sys.exit(1)
    
      reduced = Reduced_Grid(grid_data, mode, cell_size)
    
      from VolumeData.netcdf.netcdf_grid import write_grid_as_netcdf
      write_grid_as_netcdf(reduced, outpath)
    

The exact invalid syntax error is in the "except fileformats.Uknown_File_Type,
e:" line. Can you help me ?


Answer: If you are using Python 3.x, you cannot use `except
fileformats.Uknown_File_Type, e`. The comma works as an `as` statement (in the
`try`/`except` block), so you should replace it with: `except
fileformats.Uknown_File_Type as e`.

The comma works in Python 2.7, but not 3.x. However, the `as` should work for
both.

Reference: [Handling errors in Python
3.3](http://docs.python.org/3.3/tutorial/errors.html#handling-exceptions)

