Why use Python context manager for file I/O?

Use a context manager when working with file I/O in Python to avoid exceeding resource limits. For long running jobs, context managers help avoid random crashes due to excess file I/O resource utilization from files left hanging open.

There are edge cases where you do need to keep the handle open without context manager, but in most cases I’ll let the file open and close many times with the context manager.

Context Manager examples

These examples assume you’ve done something like

from pathlib import Path

fn = Path('~/mydir/myfile').expanduser()

simple file I/O

with fn.open('r') as f:
    line = f.readline()


import h5py

with h5py.File(fn,'r') as f:
    data = f['myvar'][:]


import netCDF4

with netCDF4.Dataset(str(fn),'r') as f:
    data = f['myvar'][:]