-1

I'm trying to execute something every week in my code...

My code is below, and I need a function like "sleep_until(date)" if there is such function in Python.

Please help me!

import time
import datetime
from datetime import timedelta

while True:
    dateFrom = datetime.datetime.now()
    dateTo= dateFrom + timedelta(days=7)  
    ######################
    # my code
    ######################

    time.sleep_until(dateTo)# Expected code
sophros
  • 14,672
  • 11
  • 46
  • 75
  • is [this](https://stackoverflow.com/q/2031111/10197418) what you're looking for? – FObersteiner Jan 18 '21 at 09:39
  • Does this answer your question? [Running Python at a Certain Time with Datetime](https://stackoverflow.com/questions/26835048/running-python-at-a-certain-time-with-datetime) OR [How do I get a Cron like scheduler in Python?](https://stackoverflow.com/questions/373335/how-do-i-get-a-cron-like-scheduler-in-python) – Tomerikoo Jan 18 '21 at 09:40
  • Thank you my friends, It's work!! :) – Minonja Nandraina Razakasoa Jan 18 '21 at 09:45

1 Answers1

0

The code below is working! Thank you all import time import datetime from datetime import timedelta

while True:
    dateFrom = datetime.datetime.now()
    dateTo= dateFrom + timedelta(seconds=60)  
    #dateTo= dateFrom + timedelta(days=7)  
    
    ######################
    # my code
    ######################
    print("----------------------")
    print(dateFrom)
    print(dateTo)
    time.sleep((dateTo-dateFrom).total_seconds())