Changed f=open() to with open() as f to eliminate ResourceWarnings. Fixes #3410.
Moved code out of context block where not required for #3612
Merge pull request #3612 from calvingiles/fix-resource-warnings

[MRG+1] Fixed ResourceWarnings from inside scikit-learn. Fixes #3410.