mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-23 19:01:26 -07:00
[PR #9579/0de39a6f backport][stable-10] use open() as context manager (#9596)
use open() as context manager (#9579)
* use open() as context manager
* add changelog frag
(cherry picked from commit 0de39a6f47
)
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
parent
98d25a3e4d
commit
e9b58cfc09
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