用Python实现定时关机


Python #定时关机2012-11-25 22:29

实现代码如下:

01import os,time;
02rh=int(time.strftime("%H",time.localtime()));
03rm=int(time.strftime("%M",time.localtime()));
04cmd="cmd.exe /k shutdown -s -t 0";
05c1=True;
06while c1:
07    try:
08        h=int(raw_input("Please input the hour:"));
09        if h>=0 and h<=23:
10            c1=False;
11        else:
12            continue;
13    except:
14        continue;
15c2=True;
16while c2:
17    try:
18        m=int(raw_input("Please input the minute:"));
19        if m>=0 and m<=59:
20            c2=False;
21        else:
22            continue;
23    except:
24        continue;
25if h==rh:
26    if m<=rm:
27        os.system(cmd);
28    else:
29        time.sleep((m-rm)*60);
30        os.system(cmd);
31elif h>rh:
32    tem1=(h-rh-1)*3600+(60-rm+m)*60;
33    time.sleep(tem1);
34    os.system(cmd);
35else:
36    tem2=(23-rh+h)*3600+(60-rm+m)*60;
37    time.sleep(tem2);
38    os.system(cmd);


相关文章

粤ICP备11097351号-1