title: How to read and write a text file in the same time in python?
body: <p>I am trying to open a text file, read it and after using a regex function to find which lines to edit, modify my text file. However what happens is that after finding the lines and edit them i can not write the modified Content in the text file again. </p>
  
  <pre><code>remove_commas = re.compile("House")
  answer = {}
  
  global line1
  
  with open("\DEMO_houses.txt", "r") as inp:
  
      for line in inp:
          if remove_commas.match(line):
  
              line1 = line.replace(',', '')
              print line1
  
  with open("DEMO_houses.txt", "w") as document1:
          document1.write(line1)
  </code></pre>
  
  <p>What happens is that it just delets my text file, and writes only the first line modified.</p>
  
  <p>Text file is something like this:</p>
  
  <pre><code>Street : Blue, Red
  House: Big, enough
  Garden : green, not green
  </code></pre>
  
  <p>And in the new text file i Need something like:</p>
  
  <pre><code>Street : Blue, Red
  House: Big enough
  Garden : green, not green
  </code></pre>
  
  <p>If anyone could help me I would really appreciate it. Thanks </p>