<gh_stars>1000+

f = open("filename")
try:                         # Method of ensuring file closure
    f.write(...)
finally:
    f.close()


with open("filename") as f:  # Simpler method of ensuring file closure
    f.write(...)
f = open("filename")
try:                         # Method of ensuring file closure
    f.write(...)
finally:
    f.close()


with open("filename") as f:  # Simpler method of ensuring file closure
    f.write(...)
# Method of ensuring file closure # Simpler method of ensuring file closure