Use a context manager to ensure closing file

This will ensure that the resource is always closed, even if there
is an exception. Normal files support this out of the box, but urlopen
doesn't (until python 3), so we have to wrap it.

See https://docs.python.org/2/library/contextlib.html
Use enumerate instead of counting manually

Faster, more pythonic, easier to read, less bookkeeping