title: Writing to a new file if it doesn't exist, and appending to a file if it does
body: <p>I have a program which writes a user's <code>highscore</code> to a text file. The file is named by the user when they choose a <code>playername</code>. </p>
  
  <p>If the file with that specific username already exists, then the program should append to the file (so that you can see more than one <code>highscore</code>). And if a file with that username doesn't exist (for example, if the user is new), it should create a new file and write to it.</p>
  
  <p>Here's the relevant, so far not working, code:</p>
  
  <pre><code>try: 
      with open(player): #player is the varible storing the username input
          with open(player, 'a') as highscore:
              highscore.write("Username:", player)
  
  except IOError:
      with open(player + ".txt", 'w') as highscore:
          highscore.write("Username:", player)
  </code></pre>
  
  <p>The above code creates a new file if it doesn't exist, and writes to it. If it exists, nothing has been appended when I check the file, and I get no errors.</p>