Working with files safely
Python file objects support reading, writing and iteration. The with statement is the normal pattern because it closes the file automatically when the block finishes, including when an exception occurs.
Prefer pathlib for paths
pathlib.Path provides a readable object-oriented interface for paths and common file operations. Always think about whether a path is relative to the current working directory or an explicit application location.
Large files need a different strategy
Reading an entire multi-gigabyte file into memory is inefficient. Iterate over the file line by line when processing large text files. Use binary mode for binary data such as images.
Practice: create a log file, append several lines, then read it line by line and count lines containing ERROR.