close csv file after creating media object
@@ -13,6 +13,7 @@ Media class.
"""
from numbers import Number
+from pathlib import Path
import warnings
import numpy as npy
@@ -1463,17 +1464,20 @@ class DefinedGammaZ0(Media):
write_csv
"""
try:
- f = open(filename)
+ fid = open(filename)
except(TypeError):
# they may have passed a file
- f = filename
+ fid = filename
- header = f.readline()
+ header = fid.readline()
# this is not the correct way to do this ... but whatever
f_unit = header.split(',')[0].split('[')[1].split(']')[0]
f,z_re,z_im,g_re,g_im,pz_re,pz_im = \
- npy.loadtxt(f, delimiter=',').T
+ npy.loadtxt(fid, delimiter=',').T
+
+ if isinstance(filename, (str, Path)):
+ fid.close()
return cls(
frequency = Frequency.from_f(f, unit=f_unit),
