-2

I got a discord Bot in which I want to implement a own logging System. The problem here is that I check if I want to check if the bot was shut down in the last log, it detects it but still does something wrong of which I don't really know what.

This is the relevant code, added some prints for debugging:

for file in get_log_files(path):  # get_log_files yields all log files in the path
    with open(path+file, "r", encoding="cp1252") as f:
         print(not f.read().endswith('Bot wird heruntergefahren'))
         if not f.read().endswith('Bot wird heruntergefahren'):
            return file
         else: print(1)

If I execute my bot, which runs at start through the log in on_ready this function, it reads the last log correctly and if shut down before, prints out a False, if not a True. Here now comes the problem:

Doesn't matter which of the two were printed, it never prints the 1 afterwards: Screenshot of the console Another screenshot of the console

FileX
  • 773
  • 2
  • 7
  • 19

1 Answers1

0

The problem here is that you call f.read() twice, so the second read will return an empty string because the end of the file was reached after the first read.

What you can do instead is save the output of f.read().endswith('Bot wird heruntergefahren') in a variable before printing it and using it in your condition.

Victor Deplasse
  • 682
  • 6
  • 10