diff --git a/autotest/test_mf6.py b/autotest/test_mf6.py index 6cc9e9c3ef..4655515708 100644 --- a/autotest/test_mf6.py +++ b/autotest/test_mf6.py @@ -945,6 +945,7 @@ def test_output(tmpdir, example_data_path): bud = ml.oc.output.budget() budcsv = ml.oc.output.budgetcsv() + assert budcsv.file.closed hds = ml.oc.output.head() lst = ml.oc.output.list() diff --git a/flopy/utils/observationfile.py b/flopy/utils/observationfile.py index 1e084f846f..525dbb6dc7 100644 --- a/flopy/utils/observationfile.py +++ b/flopy/utils/observationfile.py @@ -509,20 +509,20 @@ def __init__( self, csvfile, delimiter=",", deletechars="", replace_space="" ): - self.file = open(csvfile, "r") - self.delimiter = delimiter - self.deletechars = deletechars - self.replace_space = replace_space - - # read header line - line = self.file.readline() - self._header = line.rstrip().split(delimiter) - self.floattype = "f8" - self.dtype = _build_dtype(self._header, self.floattype) - - self.data = self.read_csv( - self.file, self.dtype, delimiter, replace_space - ) + with open(csvfile, "r") as self.file: + self.delimiter = delimiter + self.deletechars = deletechars + self.replace_space = replace_space + + # read header line + line = self.file.readline() + self._header = line.rstrip().split(delimiter) + self.floattype = "f8" + self.dtype = _build_dtype(self._header, self.floattype) + + self.data = self.read_csv( + self.file, self.dtype, delimiter, deletechars, replace_space + ) @property def obsnames(self):