当前位置:Gxlcms > Python > python实现的重启关机程序实例

python实现的重启关机程序实例

时间:2021-07-01 10:21:17 帮助过:116人阅读

本文实例讲述了Python实现的重启关机程序的方法,对Python程序设计有一定的参考价值。具体方法如下:

实例代码如下:

  1. #!/usr/bin/python
  2. #coding=utf-8
  3. import time
  4. from os import system
  5. runing = True
  6. while runing:
  7. input = raw_input('关机(s)OR重启(r)?(q退出)')
  8. input = input.lower()
  9. if input == 'q' or input =='quit':
  10. runing = False
  11. print '程序退出'
  12. break
  13. seconds = int(raw_input('请输入暂停时间(单位:秒):'))
  14. time.sleep(seconds)
  15. print '暂停时间:', seconds
  16. runing = False
  17. if input == 's':
  18. print '关机ing'
  19. system('halt')
  20. elif input == 'r':
  21. print '重启ing'
  22. system('reboot')
  23. else:
  24. print '程序错误重新输入'
  25. runing = True
  26. print '程序结束~~~!'

该实例在linux下测试通过,windows的话需要判断执行命令。

人气教程排行