Using Python to change the date on an xml document
<p>I have an xml in this form</p>

<pre><code>&lt;project name="Hello World"&gt;
        &lt;testfile name="testfile1"&gt;
                &lt;type&gt;TXT&lt;/type&gt;
                &lt;size&gt;1000&lt;/size&gt;
                &lt;lastModified&gt;2014-08-03 03:40:00&lt;/lastModified&gt;
        &lt;/testfile&gt;
        &lt;testfile name="testfile2"&gt;
                &lt;type&gt;PDF&lt;/type&gt;
                &lt;size&gt;500&lt;/size&gt;
                &lt;lastModified&gt;2015-09-23 17:40:17&lt;/lastModified&gt;
        &lt;/testfile&gt;
&lt;/project&gt;
</code></pre>

<p>This is an .xml file containing info about my project, so I can update my testfiles should they are more than 3 months old. </p>

<p>Right now, i'm stuck trying to figure out how to change the element in the .xml file. This is my code so far...</p>

<pre><code>import xml.etree.ElementTree as ET
import sys
from datetime import datetime
def updateLastModified(self):
        today = datetime.now().strftime('%Y-%m-%d %H:%M:%S') # This variable stores todays date and time stamp for future reference. We shouldn't compute a new one every time.
        today = datetime.strptime(today, '%Y-%m-%d %H:%M:%S') 
        # Now we need to iterate through all the testfiles in our metadata and update their lastModified tag with the current date.
        for testfile in self.getMetadataRoot().findall('testfile'):
            lastmodified = testfile.find('lastModified')    # get the lastmodified field in it's whole, so we can modify it.
            previous_update = datetime.strptime(lastmodified.text, '%Y-%m-%d %H:%M:%S') # get the previous date from the lastmodified field and translate it from the str format

            if previous_update &lt; today:
                lastmodified.text = str(today.strftime('%Y-%m-%d %H:%M:%S'))
                self.getMetadataTree().write(self.meta_file)
</code></pre>

<p>But for some reason, the meta_file is not changing... What am I doing wrong???</p>

<p>The problem is after the <code>if</code> statement, where the file is not being modified</p>

<p>here are the other methods i'm using in this class:</p>

<pre><code>def __init__(self, filepath):
        self.meta_file = filepath

def getMetadataTree(self):
    return ET.parse(self.meta_file)

def getMetadataRoot(self):        
    tree = self.getMetadataTree()
    root = tree.getroot()
    return root 
</code></pre>