Module 7

with open(...)

A safer way to work with files.

Automatic cleanup

The with statement opens a file and automatically closes it when you are done. This is the recommended way to work with files.

PythonUsing with (conceptual).
# Conceptual example:
# with open("data.txt") as f:
#     content = f.read()
# print(content)
Try it

Using with ensures the file is closed properly, even if an error occurs.