title: Writing to file in a specific position error Python 3
body: <p>I have an XML file where I need to add the tag 'document' after the  tag which is the 3rd line in the file. So I need to be adding the 'document' tag in the 4th line of the file.
  So far, the code that I have written to do so is as follows-</p>
  
  <pre><code># search for element within xml file using regex-
  file = open("path_to_file/5.xml", "r")
  while True:
      line = file.readline()
      match = re.search(r'&lt;!DOCTYPE .+', line)
      if match:
          print("Pattern found: ", match.group())
          print("Current file pos: ", file.tell())
          break
  
  
  # Pattern found:  &lt;!DOCTYPE article SYSTEM "../article.dtd"&gt;
  # Current file pos:  199
  
  file.close()
  
  
  # open xml file in append mode and write element/tag to file-
  file = open("path_to_file/Desktop/5.xml", "a")
  
  file.seek(199)
  # 199
  
  file.tell()
  # 199
  
  # write element/tag to xml file-
  file.write('\n\n\n&lt;document&gt;\n\n\n')
  
  # close file-
  file.close()
  </code></pre>
  
  <p>But this is not doing the appropriate changes to the file as I am expecting it to. What's going wrong?</p>
  
  <p>Thanks!</p>