Trying to open and create a new file, then changing all 4 letter words to "xxxxxx"
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/13284291/replace-four-letter-word-in-python">Replace four letter word in python</a>  </p>
</blockquote>



<p>For homework, I need to open a file, replace all four letter words with "xxxxxx", then write the text into a new file.</p>

<p>This is the file text in the original file that was given:</p>

<pre><code>The 3 lines in this file end with the new line character.

There is a blank line above this line.
</code></pre>

<p>This is what I have so far:</p>

<pre><code>def censor(filename):

    infile=open(filename,"r")

    content=infile.read()infile.close()

    outfile = open("censored.txt","w")

    content=content.replace("this","xxxxxx")

    content=content.replace("file","xxxxxx")

    content=content.replace("with","xxxxxx")

    content=content.replace("line","xxxxxx")

    outfile.write(content)

    outfile.close()
</code></pre>

<p>This is the result:</p>

<pre><code>The 3 xxxxxxs in xxxxxx xxxxxx end xxxxxx the new xxxxxx character.

There is a blank xxxxxx above xxxxxx xxxxxx.
</code></pre>

<p>I'm having trouble getting just "line" and not "lines" to change, since at the moment "lines" is changing to "xxxxxxs".</p>

<p>Does anyone know of a particular way to do this? Will an if statement be needed?</p>
