问题:在使用schedule定时运行程序时,需要对程序自动运行的次数进行统计,如何进行计数叠加,未找到特别合适的方法。
初步解决方法:将计数部分存入txt文件,然后再读取计算,初步实现最初想法。
实现代码:
import schedule
def fun_counter(path):
with open(path,'r') as f:
n = f.read()
if n=='':
n=0
with open(path,'w') as f:
n=int(n)+1
f.write(str(n))
else:
with open(path,'w') as f:
n=int(n)+1
f.write(str(n))
print(f'程序目前运行了{n}次')
def job(path):
print('do job')
fun_counter(path)
path = 'test.txt'
schedule.every(0.01).minutes.do(job,path=path)
while True:
schedule.run_pending()