Replace specific text with a block of text
<p>I am trying to replace a small section of text with a block of text. I am hoping that this block of text will be replaced using replace function of python. I would have really like to use <code>sed</code> to get this job, but I am on windows machine. Anyway here is my script, not sure why the block of text isn't being replaced. </p>

<pre><code># Change the current working directory to IDX location
import os
print os.getcwd()
os.chdir(##location of my text files##) 
print os.getcwd()

#Read file names

com_line=raw_input("File name please:")
contents=[]
strs = '#DREFIELD IDOL_SOURCE="idol source"\n#DREFIELD IDOL_TAXONOMY="taxonomy data"\n #DREFIELD IDOL_CATEGORY="IDOL_CATEGORY"\n #DREFIELD IDOL_PROMOTION="idol_promotion"\n #DREFIELD IDOL_PROMOTION_TERM="IDOL_PROMOTION_TERM"\n #DREFIELD IDOL_WHATSNEW="IDOL_WHATSNEW"\n #DREFIELD IDOL_SUMMARY="IDOL_SUMMARY"\n #DREFIELD IDOL_URL="IDOL_URL DATA"\n #DREFIELD IDOL_FORMNUMBER="IDOL_FORMNUMBER"\n #DREFIELD IDOL_DESCRIPTION="IDOL_DESCRIPTION DATA"\n#DRESECTION 0\n'
with open(com_line) as f:
     contents.append(f.readlines())
f.close

#Writing to file
f2 = open("test.idx", 'w')

for content in contents:
    for s in content:
        if s == '#DRESECTION 0\n':
            s.replace("#DRESECTION 0\n", strs)
        f2.write("%s" % s)
f2.close
</code></pre>

<p>I tested this code, and it is entering the conditional statement as well. Just wondering why is the needful text <code>#DRESECTION 0\n</code> not getting replaced with <code>strs</code> ? Also I am looking to do this operation of large text files worth few gigs. Is there a better way of reading large text files in python?</p>

<p>Stings in Python are immutable. Once a string object has been created, it can't be modified. You can however create a new string based on the old one.</p>

<p>When you are calling <code>s.replace</code>, it doesn't modify <code>s</code> in place, because that's not possible. Instead, the <code>str.replace</code> method returns a new string. You could do <code>s = s.replace(...)</code> and it should work. However, there's no point in replacing the whole contents of one string with another string when you could just rebind the name to the new string.</p>

<p>I suggest using:</p>

<pre><code>    if s == '#DRESECTION 0\n':
        s = strs
    f2.write(s)
</code></pre>

<p>I also changed the <code>write</code> call. There's no need to use string substitution here, since you want to write the whole string (and nothing extra). Just pass <code>s</code> as the argument.</p>

<p>There are a few other minor issues with your code:</p>

<p>For instance, you're not calling the <code>close</code> methods on your files, only naming them. You need <code>f2.close()</code> if you want to actually close the file. But you don't actually need to do that for <code>f</code>, since the <code>with</code> statement you're using will close the file automatically (you should probably use a <code>with</code> for <code>f2</code> and not bother with the manual <code>close</code> call).</p>

<p>There also doesn't seem to be much need <code>contents</code> to be a nested list (with just one item in the outer list). You probably want to do <code>contents = f.readlines()</code> rather than <code>append</code>ing the list of lines to an initially empty list. That will let you get rid of one of the loops in the later code.</p>

python replace readline