Python Tip #26 (of 365):
When opening a file, use a "with" block... especially for writing!
Using a "with" block to open a file will close it automatically when the "with" block exits: https://pym.dev/creating-and-writing-file-python/
So this:
with open("example.txt", mode="wt") as file:
file.write("An example file\n")
Is pretty much the same as this:
file = open("example.txt", mode="wt")
try:
file.write("An example file\n")
finally:
file.close()
๐งต(1/4)
