title: Search for first occurrence of a string and insert string on line above with python
body: <p>I usually work in bash so i'm very new to this frightening world of python.</p>
  
  <p>I am attempting to search a file for a string then insert text above the "First occurrence" of that string with empty line between.  </p>
  
  <p>The file to be edited would look like this:</p>
  
  <pre><code>Name:
  
  Billy
  
  Thorton
  
  Billy
  
  Thorton
  </code></pre>
  
  <p>I am trying to insert "Bob" above "Thorton" with the empty lines between like this:</p>
  
  <pre><code>Name:
  
  Billy
  
  Bob
  
  Thorton
  
  Billy
  
  Thorton
  </code></pre>
  
  <p>This is the Python i have so far.</p>
  
  <pre><code>contents = "Bob"
  f = open("file", "w")
  contents = "".join(contents)
  f.write(contents)
  f.close()
  </code></pre>
  
  <p>This does not search for the string and it replaces the whole file.</p>
  
  <p>A working example in bash would be:</p>
  
  <pre><code>sed  -i '0,/Thorton/s//Bob\n\n&amp;/' file
  </code></pre>