Handle more file closing with "with"

This commit is contained in:
Ville Skyttä
2016-07-07 18:53:34 +03:00
parent 593708a39c
commit e8a8be5788
6 changed files with 17 additions and 33 deletions

View File

@@ -223,11 +223,8 @@ def main(argv):
print("Checking %s..." % fn)
try:
f = open(fn, 'rb')
try:
with open(fn, 'rb') as f:
lines = list(f)
finally:
f.close()
except (IOError, OSError) as err:
print("%s: cannot open: %s" % (fn, err))
num += 1

View File

@@ -127,8 +127,8 @@ def check(file):
errprint("%s: I/O Error: %s" % (file, str(msg)))
return
r = Reindenter(f)
f.close()
with f:
r = Reindenter(f)
if r.run():
if verbose:
print("changed.")
@@ -140,9 +140,8 @@ def check(file):
shutil.copyfile(file, bak)
if verbose:
print("backed up", file, "to", bak)
f = open(file, "w")
r.write(f)
f.close()
with open(file, "w") as f:
r.write(f)
if verbose:
print("wrote new", file)
return True