首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >python实现程序重启和系统重启

python实现程序重启和系统重启

作者头像
py3study
发布2020-01-08 18:13:14
发布2020-01-08 18:13:14
3.6K0
举报
文章被收录于专栏:python3python3
代码语言:javascript
复制
def restart_program():
    """Restarts the current program.
    Note: this function does not return. Any cleanup action (like
    saving data) must be done before calling this function."""
    python = sys.executable
    os.execl(python, python, * sys.argv)
代码语言:javascript
复制
机器重启
代码语言:javascript
复制
#!/usr/bin/python
#coding=utf-8
import time
from os import system
runing = True
while runing:
  input = raw_input('关机(s)OR重启(r)?(q退出)')
  input = input.lower()
  if input == 'q' or input =='quit':
    runing = False
    print '程序退出'
    break
  seconds = int(raw_input('请输入暂停时间(单位:秒):'))
  time.sleep(seconds)
  print '暂停时间:', seconds
  runing = False
  
  if input == 's':
    print '关机ing'
    system('halt')
  elif input == 'r':
    print '重启ing'
    system('reboot')
  else:
    print '程序错误重新输入'
    runing = True
print '程序结束~~~!'
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/09/04 ,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档