Home > Back-end >  Python datetime i need specific 30 second execute for loop
Python datetime i need specific 30 second execute for loop

Time:05-15

i need python loop condition execute every 1 minutes specific seconds loop run for an example now time 15:50:12 that time for loop run 15:40:30 next wait local time 30 second reach. Need run that correct time. 15:41:30, 15:42:30, 15:43:30

import pandas as pd
from datetime import datetime

df = pd. read_excel('data.xlsx')
Time30 = now.replace(hour=00, minute=00, second=30)

for i in df.index:
    entry = df.loc[i]
    now = datetime.now()
    current_time = now.strptime('%S')

    current_time == Time30

    name_input = browser.find_element_by_name('fullname')
   password_input = browser.find_element_by_name('password")


`````````

CodePudding user response:

If your function takes time, you can use this

import time
from datetime import datetime


def main():
    # Do something
    ...


now = datetime.now().timetuple()
next_time = time.mktime(datetime(now[0], now[1], now[2], now[3], 30).timetuple())
while next_time < time.time():
    next_time  = 30

while True:
    time.sleep(next_time - time.time())
    main()
    next_time  = 30

CodePudding user response:

From what I could gather, this should solve your problem

from time import sleep

# wait until seconds equal 30
while datetime.now().strftime("%H:%M:%S").split(":")[2] != "30":
    pass

for i in range(10):
    print(datetime.now().strftime("%H:%M:%S"))
    # sleep for 1 minute in every iteration
    sleep(60)

You will need to modify this to suit your cause, but this is the general idea.

Thanks to @Pranav in the comments the first while loop can be optimised as follows

while datetime.now().second != 30:
    sleep(1)
  • Related