krystosan 0 Junior Poster

How should I make an entry appended to existing entries instead of overwriting the whole file ?

class ReadWriteCustomPathsToDisk(object):
    """docstring for ReadWriteCustomPathsToDisk"""
    def __init__(self, modulePath):
        super(ReadWriteCustomPathsToDisk, self).__init__()
        self.modulePath = modulePath

    def _xmlFileLocation(self):
        xmlFileLocation =  os.path.join(os.path.expanduser("~"), "Documents","searchMethod","modules.xml")
        return xmlFileLocation
        if os.path.exists(xmlFileLocation):
            return xmlFileLocation

    def readXml(self):
        xmlFile = self._xmlFileLocation()
        root = etree.parse(xmlFile).getroot()
        modPath = {}
        lst = root.getchildren()
        for module in lst:
            modPath[module.attrib['name']] = module.text
        return modPath


    def _entryExist(self):
        if self.modulePath in self.readXml().values():
            return True
        else:
            return False

    def addEntry(self):
        path = self.modulePath
        root = Element("modules")
        tree = ElementTree(root)
        childPath = Element("module")
        childPath.set("name", os.path.basename(path))
        root.append(childPath)
        childPath.text = path
        return tree, root

    def updateXml(self):
        tree,root = self.addEntry()
        print "Saving to %s " % self._xmlFileLocation()
        tree.write(open(self._xmlFileLocation(),'w'))

the main program creats initiates this class and checks if _entryExist returns true else I want to append the entry.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.