how to append an entry to existing entries

A forum for general discussion of the Python programming language.

how to append an entry to existing entries

Postby sanfx » Thu Oct 31, 2013 4:31 pm

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

Code: Select all
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'))
sanfx
 
Posts: 3
Joined: Thu Oct 31, 2013 4:22 pm

Re: how to append an entry to existing entries

Postby Crimson King » Thu Oct 31, 2013 7:00 pm

Hello sanfx,

i don't have much time right now to check all your code but one thing that comes to mind is
Code: Select all
tree.write(open(self._xmlFileLocation(),'w'))


The 'w' in the open function means you want to truncate the file to 0 or to create it.

Instead use 'a', this opens the file for writing and places the stream at the end of it.

Check out Open File modes if you want to know more about it.

Good luck, let us know if that helped you.
Ubuntu 13.04, Archlinux and Windows 7.

Python 2.7.3
User avatar
Crimson King
 
Posts: 57
Joined: Fri Mar 08, 2013 2:42 pm
Location: Buenos Aires, Argentina

Re: how to append an entry to existing entries

Postby sanfx » Fri Nov 01, 2013 5:39 am

thats ok, but this way the XML will not be structured correctly, there can't be more than one root.
what i want:
example

Code: Select all
<modules>
   <module name="cpp2python">/Users/jhavik/Development/python/cpp2python</module>
   <module name="megha">/Users/jhavik/Development/python/megha</module>
   <module name="ultron">/Users/jhavik/Development/python/ultron</module>
</modules>


but what i am getting is and below entry gets overwritten
Code: Select all
<modules>
   <module name="cpp2python">/Users/sanjeevkumar/Development/python/cpp2python</module>
</modules>

however how you are telling me will lead to multiple root (modules)
Code: Select all
<modules>
   <module name="cpp2python">/Users/sanjeevkumar/Development/python/cpp2python</module>
</modules>
<modules>
      <module name="megha">/Users/jhavik/Development/python/megha</module>
</modules>
<modules>
   <module name="ultron">/Users/jhavik/Development/python/ultron</module>
<modules>
sanfx
 
Posts: 3
Joined: Thu Oct 31, 2013 4:22 pm


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 3 guests