-1

im trying to make a command happen when its a certain time but its not working someone please help thanks, here is my code


from datetime import datetime
import time

while True:

    sam = (datetime.now().time())
    ssam = str(sam)

    if ssam == ('23:12:24.092411'):
        print("hui")


    else:
       pass
Samuel Okasia
  • 95
  • 1
  • 6

1 Answers1

0

The odds of calling datetime.now().time() at a precise microsecond are slim. One-second accuracy would probably be sufficient, and if so, you don't want to call datetime.now().time() as fast as you can; even a one-second delay in the loop saves thousands of unnecessary calls.

import datetime
import time

at_time = datetime.time(23, 14, 12)

while datetime.datetime.now().time() < at_time:
    time.sleep(1)

print("hui")
chepner
  • 497,756
  • 71
  • 530
  • 681
  • while datetime.now().time() < at_time: AttributeError: module 'datetime' has no attribute 'now' Process finished with exit code 1 – Samuel Okasia Feb 06 '20 at 18:19
  • Fixed; I was only importing the module, not the class from the module, and forgot to adjust the call to `now` appropriately. – chepner Feb 06 '20 at 18:46