Replaced useless statements by context managers

There were several useless memory-wasting statements in initialization
code which leaked file descriptors. Python offers context managers for
IO objects to prevent any leaking and enforce closing them at end.

Furthermore there was a useless and memory-wasting read() on some file
objects, although the json module offers an interface to read from IO
objects, which is less memory-wasting. This was also replaced.