I have a definition where it will write a file and put all the characters and their occurrences and then sorts it and I run into this error where one of my lines of code can't do anything because nothing has been written to the file yet, I put the file write before this and it doesn't write unless I comment out the section that sorts it after
  file = open("leaderboard.txt", "w+")
  def always_write_the_file(x):
    while x < len(collected_people):
      file.write("%s %s\n" % (collected_people[x], collected_characters.count(collected_people[x])))
      x = x + 1
  
  always_write_the_file(x)
  x = 0
  with open("leaderboard.txt") as  f:
    leaderboard = f.read().splitlines()
  while x < len(collected_people):
    character_count_turtle.penup()
    character_count_turtle.goto(non_depth, depth)
    character_count_turtle.pendown()
    
    sorted_leaderboard = sorted(leaderboard, key=lambda x: int(x.rsplit(".", maxsplit=1)[-1]))
    
    character_count_turtle.write("%s" % (sorted_leaderboard[x]), font=("arial", int(leaderboard_size), "normal"))
    depth = depth - (int(leaderboard_size) + 2)
    x = x + 1
the error I get is
probably because nothing has been written to the file...
there for sure is stuff written here though



 
    