Reputation: 169
I want my Python code to execute a function every week day (Monday to Friday) and not on weekends (Saturday and Sunday)
Is there an easy way to achieve this using the schedule library?
So far I have manage to execute the function every day, every 10 seconds. But this will also execute it on Saturdays and Sundays
def helloWorld():
print("Hello World!!!")
schedule.every(10).days.seconds.do(helloWorld)
while True:
schedule.run_pending()
time.sleep(1)
Upvotes: 0
Views: 4502
Reputation: 11
one way to do it is to replace your
.days with .monday through .friday
That way you could shedule 5 Task that run on Monday through Friday and all 5 jobs would do nothing on all other days.
for reference for some examples with monday instead of days https://schedule.readthedocs.io/en/stable/
Upvotes: 1
Reputation: 3048
You can do an if statement that checks the weekday (Monday is 0):
if datetime.datetime.now().weekday() < 5:
do_something()
I agree with Ankit that a cron job might be more stable.
Upvotes: 1