仅在特定时间之间运行命令,否则运行其他命令

Con*_*nor 2 python

我想要做的是在特定时间之间更改我的墙纸。例如,我有一张在家里使用的墙纸,以及一张在学校使用的墙纸(这是一台笔记本电脑)。我想要做的是有一个 python 脚本,它运行一个 shell 命令,该命令仅在工作日和 08:00 到 14:15 之间设置我的墙纸。

我正在使用 python 3.13 和 arch linux。我遇到麻烦的部分是找到一周中的哪一天,并比较时间。我知道如何通过 python(os.system('command')) 运行 shell 命令。

Woo*_*ble 6

import datetime

day_of_week = datetime.date.today().weekday() # 0 is Monday, 6 is Sunday
time = datetime.datetime.now().time()

if day_of_week < 5 and (time > datetime.time(8) and time < datetime.time(14,15)):
    do_something()
Run Code Online (Sandbox Code Playgroud)

  • Python 允许你这样做:`... and datetime.time(14,15) &gt; time &gt; datetime.time(8):` (2认同)