Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ElementTree.parse() from file does not close the file

I have a xml file - kind of template to fill within the parameters and make a request (create some data).

I open this file with

tree = ET.parse(path_to_file)

and I make loop to get the xml from file, fill with the parameters and send a request. But after 2555 requests I get an error message:

IOError: [Errno 24] Too many open files: 'resources/cmr/skeletons/man/CreateLiveEvent.xml'

Is there a way to close file after ET.parse() opens it?

Thanks

like image 381
SergiiKozlov Avatar asked Jan 22 '15 12:01

SergiiKozlov


Video Answer


2 Answers

Upgrade your 2.7 installation. This supposedly was fixed in issue #7334, and was included in 2.7.3. It does look like there is a bug in the way the cElementTree implementation closes files however (e.g. it doesn't close them).

The alternative is to open the file yourself:

with open(path_to_file, 'rb') as xml_file:
    tree = ET.parse(xml_file)

and leave it to the with statement to close the file object. Open the file as binary; it is the job of the XML parser to handle line endings and encodings.

like image 76
Martijn Pieters Avatar answered Oct 14 '22 07:10

Martijn Pieters


You could open the file yourself and close it:

source = open(path_to_file)
tree = ET.parse(source)
... do your work ...
source.close()
like image 43
Bernhard Avatar answered Oct 14 '22 06:10

Bernhard