import datetime
import time
def time_printer():
now = datetime.datetime.now()
ts = now.strftime("%Y-%m-%d %H:%M:%S")
print('do func time: ', ts)
def loop_monitor():
while True:
time_printer()
time.sleep(5)
if __name__ == '__main__':
loop_monitor()
调度模块schedule实现定时任务
import schedule
import time
def job():
print("I'm working...")
schedule.every(10).seconds.do(job)
schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every(5).to(10).minutes.do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)
schedule.every().minute.at(":17").do(job)
while True:
schedule.run_pending()
time.sleep(1)
# 装饰器:通过 @repeat() 装饰静态方法
import time
from schedule import every, repeat, run_pending
@repeat(every().second)
def job():
print('working...')
while True:
run_pending()
time.sleep(1)
定时发微信
import schedule
from datetime import datetime, timedelta, time
import subprocess
import uiautomation as auto
import time
def read_txt():
txt_tables = []
with open("./1.txt","r",encoding="utf-8") as f:
line = f.readline() # 读取第一行
while line:
txt_tables.append(line.split("\n")[0])
line = f.readline()
return txt_tables
print(txt_tables)
list1 = read_txt()
def send():
subprocess.Popen('C:\Program Files (x86)\Tencent\WeChat\WeChat.exe')
search_object = '文件传输助手'
wechatWindow = auto.WindowControl(Name="微信", ClassName='WeChatMainWndForPC')
# 搜索
search = wechatWindow.EditControl(Name='搜索')
search.Click()
search.SendKeys(search_object)
# 给足够的时间
time.sleep(2)
wechatWindow.SendKeys('{Enter}')
# 输入框对象
edit = wechatWindow.EditControl(Name='输入')
emoji_face = list1[0]
a = list1.pop(0)
if a is not None:
edit.SendKeys(emoji_face)
# 回车发送消息
wechatWindow.SendKeys('{Enter}')
print(emoji_face)
def job():
send()
# schedule的用法
# schedule.every(10).seconds.do(job)
# schedule.every(10).minutes.do(job)
# schedule.every().hour.do(job)
# schedule.every().day.at("10:30").do(job)
# schedule.every(5).to(10).minutes.do(job)
schedule.every().monday.at("04:00").do(job)
# schedule.every().wednesday.at("13:15").do(job)
schedule.every().minute.at(":47").do(job)
schedule.every().second.until(timedelta(hours=1)).do(job)
# 也可以用装饰器的形式实现
# from schedule import every, repeat, run_pending
# @repeat(every().second)
# def job():
# print('working...')
# 取消任务
# if i == 10:
# schedule.cancel_job(job)
# 运行任务到某时间:
# schedule.every().second.until('23:59').do(job) # 今天23:59停止
# schedule.every().second.until('2030-01-01 18:30').do(job) # 2030-01-01 18:30停止
# schedule.every().second.until(timedelta(hours=8)).do(job) # 8小时后停止
# schedule.every().second.until(time(23, 59, 59)).do(job) # 今天23:59:59停止
# schedule.every().second.until(datetime(2030, 1, 1, 18, 30, 0)).do(job) # 2030-01-01 18:30停止
while True:
schedule.run_pending()