mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-18 16:31:26 -07:00
use open() as context manager (#9579)
* use open() as context manager * add changelog frag
This commit is contained in:
parent
c5cc949492
commit
0de39a6f47
13 changed files with 72 additions and 102 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue