# coding=utf-8 import datetime import time import os import psutil def run(): kill() # os.chdir(r"C:\BearBaby\ERP") path = "C:\BearBaby\ERP\BearBaby.exe" print("播放") os.system(path) def kill(): pids = psutil.pids() for pid in pids: p = psutil.Process(pid) if p.name() == 'BearBaby.exe': # print("关闭播放") cmd = 'taskkill /F /IM BearBaby.exe' os.system(cmd) def main(): while True: now = datetime.datetime.now() print(now) # 早上 start_time = datetime.datetime.strptime(str(datetime.datetime.now().date()) + '8:00', '%Y-%m-%d%H:%M') # print(start_time) # 中午 mid_time = datetime.datetime.strptime(str(datetime.datetime.now().date()) + '15:00', '%Y-%m-%d%H:%M') # print(mid_time) # 下午 last_time = datetime.datetime.strptime(str(datetime.datetime.now().date()) + '17:00', '%Y-%m-%d%H:%M') # print(last_time) # 晚上 end_time = datetime.datetime.strptime(str(datetime.datetime.now().date()) + '20:20', '%Y-%m-%d%H:%M') # print(end_time) # 当前时间 now_time = datetime.datetime.now() # 判断当前时间是否在范围时间内 if start_time < now_time < mid_time: print("播放1") run() elif mid_time < now_time < last_time: print("停止播放1") kill() elif last_time < now_time < end_time: run() print("播放2") elif end_time < now_time: kill() print("停止播放2") elif end_time < now_time: cmd = 'shutdown /p' os.system(cmd) time.sleep(1) if __name__ == '__main__': main()