用Python在Windows下实现定时关机


Python #定时关机2014-04-06 23:40

直接上代码:

~/Desktop/shutdown.py.html
"""Target OS: WinXP
Python: Ver 2.4 
Author: violet_kz ( violet_kz@gmail.com ) http://yige.org/python/
Describe: this python script call the command "shutdown.exe" to halt the
system . """
import os import sys
if __name__ == '__main__':
print '|--------------|= Shutdown =|---------------|'
print ' 0: At once ( or \'Enter\' key )'
print ' 1: After 1 hour'
print ' 2: After 2 hours'
print ' 3: Others'
print ' 5: Cancel a shutdown command'
print ' q(Q): Quit'
print '|-------------------------------------------|'
while 1: _input = raw_input('pls choose:')
if _input == 'q' or _input == 'Q':
sys.exit( 0 )
if _input in ['0','','1','2','3','5']:
break else:
print "Invaild input,pls input again :) or press \'q(Q)\' to exit"
if _input == '' or _input == '0':
time = 3 elif _input == '1':
time = 1 * 3600
elif _input == '2':
time =2 * 3600
elif _input == '3':
while 1: _hours = raw_input('After(hours) : ');
if _hours == 'q' or _hours == 'Q':
sys.exit( 1 )
if float(_hours) < 0 or float(_hours) >24:
print 'Invaild input,pls input again :) or press \'q(Q)\' to exit'
else: time = float( _hours ) * 3600
print 'ok~~, The system will be shutdowned after %s hour(s)' % _hours
break if _input == '5':
os.system( 'shutdown.exe -a' )
sys.exit( 1 )
_shutdownStr = 'shutdown.exe -f -s -t %s' % int(time)
print _shutdownStr
os.system( _shutdownStr )

其他实现可参考:http://yige.org/p/629


相关文章

粤ICP备11097351号-1