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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user