title: self modifying python script
body: <p>I want to create python script which can modify code in that script itself using <a href="https://docs.python.org/3/library/language.html" rel="noreferrer">Python Language Services</a> or using any other way.<br>
  e.g. A script which keep track of its count of successfull execution  </p>
  
  <pre><code>import re
  COUNT = 0
  
  def updateCount():
      # code to update second line e.g. COUNT = 0
      pass
  
  if __name__ == '__main__':
      print('This script has run {} times'.format(COUNT))
      updateCount()
  </code></pre>
  
  <p>On successful execution of this script code should get changed to    </p>
  
  <pre><code>import re
  COUNT = 1
  
  def updateCount():
      # code to update second line e.g. COUNT = 0
      pass
  
  if __name__ == '__main__':
      print('This script has run {} times'.format(COUNT))
      updateCount()
  </code></pre>
  
  <p>Simple approach came to my mind was to open <code>__file__</code> in write mode and do requried modification using reguler expessions etc. But that did not work I got exception <code>io.UnsupportedOperation: not readable</code>. Even if this approach would be working then it would be very risky because it can spoil my whole script. so I am looking for solution using Python Language Services. </p>