1. 程式人生 > >python電腦鬧鐘和定時執行python程式

python電腦鬧鐘和定時執行python程式

#本文也是兩塊內容

1.python電腦鬧鐘

import winsound
import time

my_hour = input('請輸入時:')
my_minute = input('請輸入分:')
print('您的鬧鈴已設定成功!等待它叫醒你吧~~~~')

while True:
    current_time = time.strftime('%H:%M', time.localtime())
    now = current_time.split(':')

    if my_hour == now[0] and my_minute == now[1]:
        winsound.Beep(600, 1000)
        break

2.定時執行python程式
用的schedule庫

import datetime
import schedule
import threading
import time
import os


def job1():
    print("I'm working for job1")
    time.sleep(2)
    print("job1:", datetime.datetime.now())


def job2(cmd):
    print("I'm working for job2")
    os.system(cmd)
    print("job2:", datetime.datetime.now())


def job1_task():
    threading.Thread(target=job1).start()


def job2_task():
    threading.Thread(target=job2,args=("python3 proudct4.py",)).start()  #args傳元組格式



schedule.every(10).seconds.do(job1_task)
schedule.every(30).seconds.do(job2_task)

while True:
    schedule.run_pending()
    time.sleep(1)