title: Python string-replace script duplicates last 1-3 lines for no apparent reason
body: <p>I'm completely helpless.
  I created a python-script in order to automaticly rename button-names in some xaml-files, but python's string replace seems to behave very weirdly.
  My script looks like this :</p>
  
  <pre><code> #Tested on python3
      import fileinput
      import csv
      import random
      import glob
      import os
  
  textToSearch = 'Button1'
  with open('dict.csv', 'r') as f: 
      reader = csv.reader(f)
      your_list = list(reader)
  
  
  
  
  
  path='F:\Release1'
  type(path)
  extension = 'xaml'
  os.chdir(path)
  result = [i for i in glob.glob('*.{}'.format(extension))]
  
  
  for i in range(0,len(result)):
      fileToSearch = path + '\\' + result[i] 
      tempFile = open( fileToSearch, 'r+' )
  
      for line in fileinput.input( fileToSearch ):
          if textToSearch in line :
              print('Match Found')
          else:
              print('Match Not Found!!')
          i=random.randint(0,717) #numbers of words in dic
          textToReplace = str(your_list[i]).replace("'","").replace("[","").replace("]","")
          tempFile.write( line.replace( textToSearch, textToReplace ) )
      tempFile.close()
      #
      #
  </code></pre>
  
  <p>When i run it, the script changes the names correctly and doesn't mess up during the process, but alway's copys some characters of the last 2-3 lines.</p>
  
  <p>Like this for example :</p>
  
  <pre><code>  &lt;/TabControl&gt;
          &lt;Label x:Name="lbStatusx" Visibility="Hidden" Content="Label" HorizontalAlignment="Left" Margin="212,51,0,0" VerticalAlignment="Top"/&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  eft" Margin="212,51,0,0" VerticalAlignment="Top"/&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>This part is a duplicate and just corrupts the file :</p>
  
  <pre><code>eft" Margin="212,51,0,0" VerticalAlignment="Top"/&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Any pointers on what i'm doing wrong?</p>
  
  <p>/edit the problem was reading and writing from the same file!
  It could either be solved by writing to a new file or by calling tempFile.truncket() before writing!</p>