From d8091363462d8e542a07f59d89f23492bd0a43bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Sat, 4 Apr 2026 18:23:34 +0200 Subject: [PATCH] BUG: ensure IO resources are properly closed after data was read from file --- dsharp_opac/dsharp_opac.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/dsharp_opac/dsharp_opac.py b/dsharp_opac/dsharp_opac.py index 9becb6a..b0ee572 100755 --- a/dsharp_opac/dsharp_opac.py +++ b/dsharp_opac/dsharp_opac.py @@ -488,9 +488,9 @@ def __init__(self, datafile, headerlines=0, reference=None): # self.datafile = datafile self.material_str = 'Optical constants from %s' % datafile - f = open(self.datafile) - self.headerinfo = [f.readline() for i in range(headerlines)] - data = np.loadtxt(f) + with open(self.datafile) as f: + self.headerinfo = f.readlines(headerlines) if headerlines > 0 else [] + data = np.loadtxt(f) # # assign wavelength and optical constants # @@ -992,9 +992,9 @@ def __init__(self): 'draine', 'eps_suvSil'), base='optical_constants') if not os.path.isfile(self.datafile): download(os.path.dirname(self.datafile)) - f = open(self.datafile) - self.headerinfo = [f.readline() for i in range(9)] - data = np.loadtxt(f) + with open(self.datafile) as f: + self.headerinfo = f.readlines(9) + data = np.loadtxt(f) # # assign wavelength and optical constants # @@ -1297,9 +1297,9 @@ def __init__(self, species, extrapol=False, lmin=None, lmax=10.0): # # read data and assign wavelength and optical constants # - f = open(self.datafile) - self.headerinfo = [f.readline() for i in range(2)] - data = np.loadtxt(f) + with open(self.datafile) as f: + self.headerinfo = f.readlines(2) + data = np.loadtxt(f) l = data[:, 0] * 1e-4 # noqa n = data[:, 1] + 1.0 k = data[:, 2]