如何让 Python 每天在特定时间运行并每小时重复一次?

问题描述

我做了一个 Python 脚本来提醒我每小时喝水。我的代码是:

import time
import plyer
from plyer import notification

if __name__ == "__main__":
    while True:
        plyer.notification.notify(
            title="Go and Drink Water!",message="You need to drink at least 3.5 litres of water Everyday. Being Dehydrated can decrease Brain Function and Energy levels.",app_icon="C:\\Users\\(My username)\\(location of icon)",timeout=10,)
        time.sleep(60 * 60)

我将 python 路径和这个脚本的路径放在一个 .bat 文件中,并使用 Task Scheduler 让它在每天早上 8 点 35 分运行。它确实在上午 8 点 35 分运行,但没有重复。谁能指出我做错了什么或提出更好的方法

解决方法

使用 python APScheduler 模块:https://apscheduler.readthedocs.io/en/stable/userguide.html#code-examples