Use safe way through "with" statement to work with files

It's good practice to use with statement to work with files. This way files get
closed automatically at the end of the block. Fixed some cases, including some
where file wasn't closed after usage

Change-Id: I9399f2a2b7edb1cd5aa64782cd9f980f9233b827
Merge "Use safe way through "with" statement to work with files"