直接上代码:
~/Desktop/shutdown.py.html """Target OS: WinXP Python: Ver 2.4 Author: violet_kz ( violet_kz@gmail.com ) https://salangsayang.com/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 )
其他实现可参考:https://salangsayang.com/p/629