Is it possible to have python 2.7 print something at a specific time of the day. For example if I ran the program at 15:06 and coded it to print "Do task now" at 15:07 it prints it. So no matter what time you ran the program once it hit 15:07 it would print "Do task now." In addition is it possible to have it print every week at this time?
-
Yes python can do that. – Stephen Rauch Jan 14 '17 at 04:20
-
Thank you @StephenRauch but how would I code this? – Ryan Jan 14 '17 at 04:22
-
I would suggest investigating the datetime module. https://docs.python.org/3/library/datetime.html – Stephen Rauch Jan 14 '17 at 04:24
-
I wouldn't use python to schedule the operation. Your operating system already has tools to run any application at a specific time – OneCricketeer Jan 14 '17 at 04:31
-
1@cricket_007 there could be good reasons the user needs this, we just don't know and we can't assume that it's for scheduling tasks. – the_constant Jan 14 '17 at 04:41
-
@Ryan will you be expecting it in a specific timezone, or will the timezone be local to the machine it's running on? – the_constant Jan 14 '17 at 04:41
-
see https://docs.python.org/2/library/sched.html – Umang Gupta Jan 14 '17 at 05:05
-
Thank you for your response @Vincenzzzochi no it will be local to the machine it is running on – Ryan Jan 14 '17 at 05:05
-
@Ryan you're welcome. Also, when do we decide that "it is currently before 15:07", meaning if we ran this at 1am today, would this be before or after 15:07? Is the cutoff midnight? By every week in your question, you mean maybe one specific day a week? – the_constant Jan 14 '17 at 05:31
-
@Vincenzzzochi Yeah so if the time was Saturday at 15:07, it would print it, once a week at that time. – Ryan Jan 14 '17 at 06:37
3 Answers
I would suggest installing the library schedule, if you're able to.
Use pip install schedule
Your code would look like this if utilizing schedule:
import schedule
import time
def task():
print("Do task now")
schedule.every().day.at("15:07").do(task)
while True:
schedule.run_pending()
time.sleep(1)
You can adjust time.sleep(1)
as necessary to sleep for longer if a 1s interval is too long. Here is the schedule library page.

- 1,245
- 1
- 12
- 18
While python is not ideal to schedule something; there are better tools out there. Yet, if this is desired to be done in python below is a way to implement it:
Prints at scheduled_time
of 11AM:
import datetime as dt
scheduled_time = dt.time(11,00,00,0)
while 1==1:
if (scheduled_time < dt.datetime.now().time() and
scheduled_time > (dt.datetime.now()- dt.timedelta(seconds=59)).time() ):
print "Now is the time to print"
break
There are two
if conditions
with an intent to print within one minute; a shorter duration can be chosen. But thebreak
immediately after
You would need to extrapolate this so that code is run across days.
Refer: datetime
Documentation

- 169
- 3
- 14
If you're not using cron
, then the general solution is to find the time remaining until you need the event to occur, have the program sleep for that duration, then continue execution.
The tricky part is to have the program find the next occurrence of a given time. There are some modules for this, but you could also do it with plain code for a well-defined case where it is only a fixed time of day.
import time
target_time = '15:07:00'
current_epoch = time.time()
# get string of full time and split it
time_parts = time.ctime().split(' ')
# replace the time component to your target
time_parts[3] = target_time
# convert to epoch
future_time = time.mktime(time.strptime(' '.join(time_parts)))
# if not in the future, add a day to make it tomorrow
diff = future_time - current_epoch
if diff < 0:
diff += 86400
time.sleep(diff)
print 'Done waiting, lets get to work'

- 3,277
- 1
- 12
- 18