According to the documentation, xarray.Dataset.close closes any files linked to this object. But the following example succeeds, unless manually closing fp: