NicolaiF
NicolaiF

Reputation: 1343

Python: Too many open files error

I'm working on an assignment were I need to to read data from 150 files and manipulate them. this needs to be done about 73 times.

count = 0    
for date in daterange(start, end):
        #Adjusting for correct format
        day = date.strftime("%d")
        month = date.strftime("%m")

#appending files
filenames.append(glob.glob('*'+month+day+'filenamehere.nc'))

#Opening files
for i in filenames[count]:
        megadatalist.append(netCDF4.Dataset(i,'r'))

count += 1

if count % interval == 0:
    ~calculation stuff here

after doing the operations needed I attempt to empty the list and close the datasets:

for i in megadatalist:
    i.close
megadatalist = []

However after running through about 34 of the datasets consisting of 150 files I get a Runtime Error: Too many open files

Do I not close the files correctly? Or what is going on, any help is appreciated!

Upvotes: 1

Views: 1320

Answers (1)

Simon Gibbons
Simon Gibbons

Reputation: 7194

You have a typo

for i in megadatalist:
    i.close

Should read

for i in megadatalist:
    i.close()

Upvotes: 4

Related Questions