Do NOT modify a list you are iterating over.

also NEVER use tabs in python, always 4 spaces.
Spaces, not tabs

grumble github editor...
Context managers - they close files

And fix a bug in the code - Its not just style.  the first file was never closed.  The context manager (with statement) automatically closes the file... even if there is an error!
overindented - fixed

Moved code that did not need to be in the context manager outside of it.
Merge branch 'patch-2' into patch-1
Merge branch 'patch-3' into patch-1