1

I want to start a Python script a few minutes before 5:00 AM every day, run some code and then sleep until 5:00 AM, then run the rest of the script. In theory, all I need is a loop like the pseudocode below, but I am not sure how to work with times only (no dates).

t2 = '05:00:00'
while timeNow < t2:
    # wait
LSUEngineer
  • 137
  • 2
  • 6
  • What's your OS? – m13op22 Feb 25 '19 at 20:41
  • 1
    `while time.time()-started < seconds_before_break` could work? Or use something like `time.strptime('%H:%M:%S')` to convert your string into seconds. – Torxed Feb 25 '19 at 20:43
  • Possible duplicate of [Python loop to run for certain amount of seconds](https://stackoverflow.com/questions/24374620/python-loop-to-run-for-certain-amount-of-seconds) – Hayder Ctee Feb 25 '19 at 20:43

1 Answers1

4

You can use the built in datetime module.

datetime.datetime.now() will give a tuple of the time.

you can get the components of the tuple by going.

datetime.datetime.now().hour or .month or .year

import datetime

t2 = 5
while datetime.datetime.now().hour < t2:
   #code

links https://docs.python.org/2/library/datetime.html