Utilize Python context managers for opening/closing files

In some of these cases, files were not being closed
This commit is contained in:
Corey Farwell 2015-08-21 11:15:17 -04:00
parent 7c45ff8e05
commit 2ab43bea5d
5 changed files with 18 additions and 21 deletions

View file

@ -41,18 +41,16 @@ def replaceFileIfChanged(filename, newContents):
# oldFileContents = ""
# try:
# oldFile = open(filename, 'rb')
# oldFileContents = ''.join(oldFile.readlines())
# oldFile.close()
# with open(filename, 'rb') as oldFile:
# oldFileContents = ''.join(oldFile.readlines())
# except:
# pass
# if newContents == oldFileContents:
# return False
f = open(filename, 'wb')
f.write(newContents)
f.close()
with open(filename, 'wb') as f:
f.write(newContents)
return True