LSUEngineer
LSUEngineer

Reputation: 137

How do I create a Python loop that will end at a specific time?

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

Upvotes: 1

Views: 198

Answers (1)

user11115921
user11115921

Reputation:

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

Upvotes: 4

Related Questions