Remove multiple lines from a text file after a specific string, then replace with new text
<p>I'm trying to write a script that can read several .xml files within a directory
When a specific string is found (every file has this script), I need it to delete all content after that string and replace it all with new content (this can be pulled in from another file if that's easier).</p>

<p>There are numerous lines being deleted/written here.</p>

<p>At the moment I am manually going through the files and deleting all text after the string, then saving the files and running this python script:</p>

<pre><code>import fileinput
import sys
import os

os.chdir("F:\Desktop\PyTest")
rootdir='F:\Desktop\PyTest'

for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        f=open(file, 'r')
        lines=f.readlines()
        f.close()
        f=open(file, 'a')
        f.write("\n      &lt;Text&gt;Lorem Ipsum&lt;/Text&gt;")
        f.write("\n      &lt;Text&gt;Lorem Ipsum&lt;/Text&gt;")
        f.write("\n      &lt;Text&gt;Lorem Ipsum&lt;/Text&gt;")
        f.write("\n      &lt;Text&gt;Lorem Ipsum&lt;/Text&gt;")
    f.close
</code></pre>

<p>It took me a while to piece this together from tutorials, and although I've managed to find tutorials to search for a specific string and to replace it, I haven't been able to erase all content after a string and replace with new.</p>

<p>Any advice would be greatly appreciated :)</p>

<p>Doesn't have to be in Python, but I am running a Windows environment.</p>
