Replace multi line code using python
<p>I am trying to replace some lines in HTML file using python.</p>

<pre><code>#! /usr/local/bin/python
import os,sys,string,filecmp,shutil,stat,pwd,datetime,time,copy,glob,re,getpass,commands
sys.path.insert(0,os.path.join(os.environ['ADM_TOOLS'],'llib'))
import tooldets,CSrcPrj,comnfuncs,COraConnect

patchHtmlName = 'patchmaintenance.html'
f = open (patchHtmlName, "rt")
g = open ('file3.txt', "rt")
h = open ('file4.txt', "rt")
contents = f.read()
contents1 = g.read()
contents2 = h.read()

#" ".join(contents1.split())
newJSCode = contents.replace(contents1, contents2)

fp2 = open(patchHtmlName, "w")
fp2.write(newJSCode)
fp2.close()
</code></pre>

<p>While the code for File3 is :</p>

<pre><code>}

 function fnIsValidEmailId(str)

And the code for File4 is : 

                document.getElementById("beforePage").style.display = "none";
            document.getElementById("afterPage").style.display = "block";
}
 function fnIsValidEmailId(str)
</code></pre>

<p>I want to replace the code of file3 to file4.</p>

<p>If I am trying to replace the content of file3 to a single line instead of multi line, then the code works fine and replaces it.</p>

<p>While executing the script, it does not give any error but does not show the desired output.</p>

<p>Please help</p>

<p>Try this one</p>

<pre><code>import re
...
...
newJSCode = re.sub(r'.*%s'%contents1, contents2, contents, re.DOTALL) 
</code></pre>

<p>This will replace contents1 with contents2 in contents. I'm not sure whether I undestood which one is to be replaced, but anyway if you want the opposite just change contents1 to contents2 an vice versa</p>

python