0

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

error

probably because nothing has been written to the file...

nothing in leaderboard.txt

there for sure is stuff written here though

stuff written there

GDcheerios
  • 21
  • 4
  • Please be precise about what's wrong, "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" *What error*? Provide the full error message including the stack trace. Please always provide a [mcve] – juanpa.arrivillaga May 20 '21 at 05:17
  • Your edit is great in that it conveys more information, but note that 1) Uploading images of error messages isn't great. Other users won't be able to search for the error message. Instead, you can quote it or format it as code. 2) You can just say `leaderboard.txt` is empty, and that `character_data.txt` has text. – Kraigolas May 20 '21 at 05:37
  • Note as well that when you open a file, you must call `f.close()` or you will not be able to see what you have written to it. The `with open ... as f:` statement implicitly closes the file when the `with` statement ends. Also note that you probably mean to write `open("leaderboard.txt", 'r')` at the with statement in your code, where you don't specify the open mode. From [this answer](https://stackoverflow.com/a/23566951/11659881) the default mode is to read binary (`rb`), which you probably don't want. – Kraigolas May 20 '21 at 05:42
  • ah I see this seems to fix it, thank you! – GDcheerios May 20 '21 at 06:08
  • Please [don’t post images of code or error messages.](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557) – tripleee May 22 '21 at 17:25

1 Answers1

0

You'll need to specify the open mode when calling the open() function:

  with open("leaderboard.txt", "r") as  f:
    leaderboard = f.read().splitlines()

Note that in this function:

  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

Instead of a while loop, you can use a for loop:

  def always_write_the_file(x_start):
    for x in range(x_start, len(collected_people)):
      file.write("%s %s\n" % (collected_people[x], collected_characters.count(collected_people[x])))
Red
  • 26,798
  • 7
  • 36
  • 58