Use try/finally with file opening to close the file

This commit is contained in:
Matt Martz
2015-05-11 10:29:28 -05:00
committed by Matt Clay
parent bdf9623f56
commit 42bca5398b
2 changed files with 44 additions and 28 deletions

View File

@@ -155,9 +155,11 @@ def main():
if changed and not module.check_mode:
f = open(path, 'wb')
f.write(str(crypttab))
f.close()
try:
f = open(path, 'wb')
f.write(str(crypttab))
finally:
f.close()
module.exit_json(changed=changed, msg=reason, **module.params)
@@ -173,10 +175,12 @@ class Crypttab(object):
os.makedirs(os.path.dirname(path))
open(path,'a').close()
f = open(path, 'r')
for line in f.readlines():
self._lines.append(Line(line))
f.close()
try:
f = open(path, 'r')
for line in f.readlines():
self._lines.append(Line(line))
finally:
f.close()
def add(self, line):
self._lines.append(line)