Drop usage of obsolete function "file"

file is deprecated in Python2. Also use context manager