title: In Python how can I replace a value in a text file with a new updated version of that value using a function?
body: <p>I've programmed a game in which you get the current score for the player (score) and then there is the highest achieved score (high_score) that gets saved in a .txt document, I need to be able to compare these values and if the score > high_score I need to be able to update it and I can't figure out how to, any help is appreciated. </p>
  
  <pre><code>file = open('save.txt','r+')
  saved = file.read()
  file.close()
  
  high_score = saved
  high_score = int(high_score)
  global score
  score = 21
  
  def checkscore():
      if score &gt; high_score:
          file = open('save.txt' , 'w+')
          file.write(file.read().replace(saved,str(score)))
          file.close()
      else:
          file.close()
      return
  
  checkscore()
  </code></pre>
  
  <p>This is what i've tried so far, just seems to delete whats in the document.</p>