Arcturus B Arcturus B -4 years ago 112
Python Question

OSError 24 (Too many open files) when reading bunch of FITS with astropy.io

I’m trying to load into memory a few 2 000 FITS using

astropy.io.fits
:

def readfits(filename):
with fits.open(filename) as ft:
# the fits contain a single HDU
data = ft[0].data
return data

data_sci = []
for i in range(2000):
data_sci.append(readfits("filename_{}.fits".format(i)))


However, when reaching the 1015th file,
OSError: [Errno 24] Too many open
files
is raised.

I have the same issue with:

def readfits(filename):
ft = fits.open(filename) as ft:
data = ft[0].data
ft.close()
return data


I suspect that
astropy.io.fits
does not properly close the file. Is there a
way I can force the files to be closed?

Answer Source
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download