0

I am currently trying to compare the current date and time to a date and time which are written down in another file. For some weird reason the while loop is not breaking but creating an endless loop.

This is what the test.txt file I try to compare the current date and time with contains: 29.10.2021 20:47:47

This is my code (Imagine ga is equal to the data in the test.txt ):

import time
from datetime import datetime
from datetime import timedelta

def background_checker():
    with open('test.txt','r+') as sample:
        while True:
            ga = datetime.now()
            ga = ga.strftime('%d.%m.%Y %H:%M:%S')
            print(ga, end='\r')
            line = sample.readline()
            if line == ga:#if time and date in file equal to the current time and date, the if statement should be triggered. 
                print('alarm')
                break
background_checker()

Am I doing something wrong in my code? If so I would be very glad if someone could explain to me what I've made wrong and how I can solve this problem.

14
  • 1
    Does this answer your question? a = open("file", "r"); a.readline() output without \n TL;DR when you use readline() there's going to be a trailing newline ('\n'), so the comparison will always return False. Commented Oct 29, 2021 at 19:16
  • readline() includes the trailing newline. It cannot equal a strftime without a trailing newline. Commented Oct 29, 2021 at 19:17
  • 1
    There are 2 things I want to say. 1. if the time in your test.txt file is not equal to the time.now() you will never break 2. Consider using the keyword 'in' instead ga in line if the line you read in is longer and contains formatting characters Commented Oct 29, 2021 at 19:17
  • 1
    What is the exact string? Commented Oct 29, 2021 at 19:25
  • 1
    the thing is that ga will always be increasing. What if the time in your file is in the past? Commented Oct 29, 2021 at 19:28

1 Answer 1

1

You are trying to create an alarm program but you're comparing strings using string equality. A better approach is to compare datetime objects.

import time
from datetime import datetime
from datetime import timedelta

def background_checker():
    format = '%d.%m.%Y %H:%M:%S'
    with open('test.txt','r+') as sample:
        while True:
            ga = datetime.now()
            print(ga)
            line = sample.readline()
            alarm_time = datetime.datetime.strptime(line, format)     
            if ga > alarm_time: #if time and date in file equal to the current time and date, the if statement should be triggered. 
                print('alarm')
                break
background_checker()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.