title: How do I write to the middle of a text file while reading its contents?
body: <p>First of all thanks for helping me out with moving files and for helping me in with tcl script.</p>
  
  <p>Small doubt i had with python code.. as below..</p>
  
  <pre><code>import os
  import shutil
  
  data =" set filelid [open \"C:/Sanity_Automation/Work_Project/Output/smokeTestResult\" w+] \n\
          puts $filelid \n\
          close $filelid \n"  
  
  path = "C:\\Sanity_Automation\\RouterTester900SystemTest"
  if os.path.exists(path):
      shutil.rmtree('C:\\Sanity_Automation\\RouterTester900SystemTest\\')
  
  path = "C:\\Program Files (x86)"
  if os.path.exists(path):
      src= "C:\\Program Files (x86)\\abc\\xyz\\QuickTest\\Scripts\\RouterTester900\\Diagnostic\\RouterTester900SystemTest"
  else:
      src= "C:\\Program Files\\abc\\xyz\\QuickTest\\Scripts\\RouterTester900\\Diagnostic\\RouterTester900SystemTest"
  dest = "C:\\Sanity_Automation\\RouterTester900SystemTest\\"
  
  shutil.copytree(src, dest)
  log = open('C:\\Sanity_Automation\\RouterTester900SystemTest\\RouterTester900SystemTest.app.tcl','r+')
  log_read=log.readlines()
  x="CloseAllOutputFile"
  with open('C:\\Sanity_Automation\\RouterTester900SystemTest\\RouterTester900SystemTest.app.tcl', 'a+') as fout:
          for line in log_read:
            if x in line:
              fout.seek(0,1)
              fout.write("\n")
              fout.write(data)
  </code></pre>
  
  <p>This code for copying files from one location to another, searching keyword in particular file and  writing data to file is working...</p>
  
  <p>My doubt is whenever i write.. It writes to end of file instead of current location...</p>
  
  <p>Example: Say.. I copied file from program files to sanity folder and searched for word "CloseAllOutputFile" in one of the copied file. when the word found, it should insert text in that position instead of end of file.</p>