Use with open(...) as ...: context managers

Instead of manually opening and closes file objects, use the context
manager that will properly handle opening and closing files for us, even
in the event of some error
This commit is contained in:
Sean Vig
2015-12-13 18:57:36 -06:00
parent 82c3230981
commit 4bd90d79d0
7 changed files with 234 additions and 249 deletions
+4 -6
View File
@@ -6,9 +6,8 @@ import sys
import os
for filename in sys.argv[1:]:
f = file(filename, "r")
lines = f.readlines()
f.close()
with open(filename, "r") as f:
lines = f.readlines()
newguard = "_" + os.path.basename(filename).upper().replace(".", "_") + "_"
@@ -24,7 +23,6 @@ for filename in sys.argv[1:]:
while not lines[end].strip().startswith("#endif"):
end -= 1
lines[end] = "#endif // %s\n" % newguard
f = file(filename, "w")
f.write("".join(lines))
f.close()
with open(filename, "w") as f:
f.write("".join(lines))
break