标签:
我觉得Window8有一个重大的优点就是支持休眠的功能,休眠以后再次开机可以保持原来已经打开的窗口。但是我发现我的电脑休眠以后会自己开机,也不知道怎么回事,于是我想可以利用python来实现在固定时间内自动关机或者休眠的功能。主要的思路如下:
获取当前时间->与设定的时间作比较->超出了设定时间->关机或休眠
代码如下:
#coding=utf-8 import time, os ,threading class TimeCmp: def __init__(self, TimeStart, TimeEnd): self.TimeStart=TimeStart self.TimeEnd=TimeEnd def GetNowTime(self): pass #now = time.strftime("%Y-%m-%d %H:%M:%S") def Cmp(self): LocalTime=time.localtime(time.time()) self.__TimeNow_1=LocalTime.tm_hour*3600+LocalTime.tm_min*60+LocalTime.tm_sec self.__TimeStart_1=3600*self.TimeStart[0]+60*self.TimeStart[1]+self.TimeStart[2] self.__TimeEnd_1=3600*self.TimeEnd[0]+60*self.TimeEnd[1]+self.TimeEnd[2] #print ‘self.__TimeNow_1‘,self.__TimeNow_1 #print ‘self.__TimeStart_1‘,self.__TimeStart_1 #print ‘self.__TimeEnd_1‘,self.__TimeEnd_1 if self.__TimeNow_1 > self.__TimeStart_1 and self.__TimeNow_1 < self.__TimeEnd_1: return True else: return False def run(): TimeStart=(8,0,0) TimeEnd=(23,30,00) SystemCmd=‘rundll32 powrprof.dll,SetSuspendState -t -s 60‘ #SystemCmd=‘shutdown -r‘ while True: TimeCmpResult=TimeCmp(TimeStart, TimeEnd) #print TimeCmpResult.Cmp() shutdown=not TimeCmpResult.Cmp() if shutdown: print "Computer will be off" time.sleep(float(60)) os.system(SystemCmd) else: print "Computer is on" pass time.sleep(float(600)) if __name__=="__main__": run()
也可以做成Windows服务实现自动启动,见http://www.cnblogs.com/lishuai0214/articles/4294486.html
标签:
原文地址:http://www.cnblogs.com/lishuai0214/p/4321348.html