Search code examples
pythonxmllxmlminidom

Python: Losing content while writing an xml file


I'm using python3.5 and lxml (and sometime minidom) for write and read xml files.

I've multiple process that read and write the same xml file and sometimse the file turns out to be totally blank. That happens sometime when I close a process manually.

This is an example of a function that modifies an xml:

from lxml import etree as le
file = open("generalList.xml", 'r')
tree = le.parse(file)
file.close()
for bad in tree.xpath("//unit"):
   ip = bad[0].text
   if ip == data[1]:
      bad.getparent().remove(bad)
file = open("generalList.xml", 'wb')
tree.writexml(file)
file.close()

Is there a way to avoid this problem?


Solution

  • Your example may be incomplete, but it looks like you mixed minidom and lxml methods to write the file, and that could produce a blank file, especially in your example.

    Check if you are using the write() method for lxml tree objects and writexml() for minidom objects.

    EDIT:

    to understand what could happen:

    file = open("versions.xml", 'wb')  # file is blank
    
    import time  # add this to take a moment to check the blank file in your folder
    time.sleep(60)
    
    # here,  if shit happens, you lose everything
    
    tree.write(file) # then the file is written and I/O closed
    file.close()
    

    You can add some try/except statements to avoid this effect with bugs in the code, but if you cut the process while it's writing => blank file