Minor fixes to range(len and file accesses

Change range(len( to enumerate. Slightly more pythonic way to iterate
and maintain an index over a list.

Changed cached_file = open statements to with open():. A bit
safer/cleaner way to go about opening and closing a file, as you're
guaranteed to close it, exceptions be damned.