use open() as context manager (#9579)

* use open() as context manager

* add changelog frag
This commit is contained in:
Alexei Znamensky 2025-01-22 08:50:44 +13:00 committed by GitHub
parent c5cc949492
commit 0de39a6f47
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 72 additions and 102 deletions

View file

@ -154,11 +154,8 @@ def main():
changed, reason = existing_line.opts.remove(opts)
if changed and not module.check_mode:
try:
f = open(path, 'wb')
with open(path, 'wb') as f:
f.write(to_bytes(crypttab, errors='surrogate_or_strict'))
finally:
f.close()
module.exit_json(changed=changed, msg=reason, **module.params)
@ -173,12 +170,9 @@ class Crypttab(object):
os.makedirs(os.path.dirname(path))
open(path, 'a').close()
try:
f = open(path, 'r')
with open(path, 'r') as f:
for line in f.readlines():
self._lines.append(Line(line))
finally:
f.close()
def add(self, line):
self._lines.append(line)