I'm creating a pyqgis script. In this script I use the processing.runalg command several times. The problem is that these processes are within a for loop. After several iterations, the error number 24 "too many open file" appears. How can I close the files generated by the processes? Here is an example of the code:

for i in range (1, 13000):

path1 = '/path/to/file1_' + str(i) + '.shp'
path2 = '/path/to/file2_' + str(i) + '.shp'
path3 = '/path/to/file3_' + str(i) + '.asc'
path4 = '/path/to/file4_' + str(i) + '.asc'
path5 = '/path/to/file5_' + str(i) + '.shp'
path6 = '/path/to/file6_' + str(i) + '.shp'
path7 = '/path/to/file7_' + str(i) + '.asc'
path8 = '/path/to/file8_' + str(i) + '.asc'

processing.runalg('qgis:reprojectlayer', path1,'EPSG:3004',path2)
processing.runalg('qgis:reprojectlayer', path5,'EPSG:3004',path6)

processing.runalg('saga:cubicsplineapproximation', path2,'field',3.0,20.0,5.0,140.0,'2596187,2675187,4321914,4403914',1000.0,0,path3)
processing.runalg('saga:cubicsplineapproximation', path6,'field',3.0,20.0,5.0,140.0,'2596187,2675187,4321914,4403914',1000.0,0,path7)

processing.runalg('gdalogr:cliprasterbyextent', path3,'-9999','2611187.7500000000000000,2660187.7500000000000000,4336914.1990000000223517,4388914.1990000000223517',5,4,75.0,6.0,1.0,False,0,False,None,path4)
processing.runalg('gdalogr:cliprasterbyextent', path7,'-9999','2611187.7500000000000000,2660187.7500000000000000,4336914.1990000000223517,4388914.1990000000223517',5,4,75.0,6.0,1.0,False,0,False,None,path8)
...     ...
  • I'm guessing it's somewhat related to this issue. What about assigning a name to each process and deleting this (i.e. output_1 = processing.runalg('qgis:reprojectlayer', path1,'EPSG:3004',path2, then after this line add del output_1)? Another approach could be to load the result of each process and then remove them all at the end of each iteration (i.e. QgsMapLayerRegistry.instance().removeMapLayers([layers.id()])
    – Joseph
    Commented Jan 5, 2018 at 12:03
  • @Joseph I've already tried this solution but it doesn' t work. I checked and the files are open until QGIS is closed Commented Jan 6, 2018 at 17:57

1 Answer 1


I had a similar issue when running a for loop of more than 160 itens in Python Console at QGis 2.18.17. I could manage it setting the number of files opened by process using ulimit command at Linux terminal before opening Qgis at the same terminal. Take a look at this link: https://docs.qgis.org/2.18/en/docs/user_manual/preamble/features.html#python-console I hope it can help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.