欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

python小脚本:定时提醒喝水

程序员文章站 2022-05-19 11:43:40
...
#
# -*- coding: utf-8 -*-  
import subprocess
import time,os

def createFile(filename):
    if os.path.exists(filename) == False:
        open(filename, "a").close()
    if os.path.getsize(filename):
        pass
    else:
        f = open(filename, "a")
        f.write("多喝水!!")
        f.close()
        
def getThisHours():
    timeInfo = time.localtime()
    return timeInfo[3]

def printTips(filename):
    createFile(filename);
    sub2 = subprocess.Popen(['notepad', filename]);

lastHours = getThisHours()

try:
    fileName = "Tips.txt"
    while True:
        nowHours = getThisHours()
        if(nowHours > lastHours):
            lastHours = nowHours
            printTips(fileName)
        time.sleep(660);#11分钟循环一次
except Exception as e:
    f = open("error.log", "a")
    f.write(str(e) + "\n")
    f.close()

定时提醒喝水,不用费劲的把脚本添加到计划任务了(后缀保存为.pyw就不会出现控制台窗口了)


#
# -*- coding: utf-8 -*-  
import subprocess
import time,os

def createFile(filename):
    if os.path.exists(filename) == False:
        open(filename, "a").close()
    if os.path.getsize(filename):
        pass
    else:
        f = open(filename, "a")
        f.write("多喝水!!")
        f.close()
        
def getThisHours():
    timeInfo = time.localtime()
    return timeInfo[3]

def printTips(filename):
    createFile(filename);
    sub2 = subprocess.Popen(['notepad', filename]);

lastHours = getThisHours()

try:
    fileName = "Tips.txt"
    while True:
        nowHours = getThisHours()
        if(nowHours > lastHours):
            lastHours = nowHours
            printTips(fileName)
        time.sleep(660);#11分钟循环一次
except Exception as e:
    f = open("error.log", "a")
    f.write(str(e) + "\n")
    f.close()


定时提醒喝水,不用费劲的把脚本添加到计划任务了(后缀保存为.pyw就不会出现控制台窗口了)


更多python小脚本:定时提醒喝水 相关文章请关注PHP中文网!

相关标签: python