title: Replace four letter word in python
body: <p>I am trying to write a program that opens a text document and replaces all four letter words with <em>*</em>*. I have been messing around with this program for multiple hours now. I can not seem to get anywhere. I was hoping someone would be able to help me out with this one. Here is what I have so far. Help is greatly appreciated! </p>
  
  <pre><code>def censor():
      filename = input("Enter name of file: ")
      file = open(filename, 'r')
      file1 = open(filename, 'w')
      for element in file:
          words = element.split()
          if len(words) == 4:
              file1 = element.replace(words, "xxxx")
              alist.append(bob)
          print (file)
      file.close()
  </code></pre>
  
  <p>here is revised verison, i don't know if this is much better</p>
  
  <pre><code>def censor():
      filename = input("Enter name of file: ")
      file = open(filename, 'r')
      file1 = open(filename, 'w')
      i = 0
      for element in file:
          words = element.split()
          for i in range(len(words)):
              if len(words[i]) == 4:
                  file1 = element.replace(i, "xxxx")
                  i = i+1
      file.close()
  </code></pre>