Read about reading and writing files in Python.
- Why is it recommended to use the
with
keyword when opening file objects? Why is it important to close a file even if an exception is raised? - The documentation states that, "Using with is also much shorter than writing equivalent try-finally blocks". What is the
finally
block and, if thewith
keyword did not exist, could you have used it to properly close a file after it was opened? - The documentation warns: > Calling f.write() without using the with keyword or calling f.close() might result in the arguments of f.write() not being completely written to the disk, even if the program exits successfully.