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

yipeiwu_com5年前Python基础

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

实例代码如下:

#!/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 '程序结束~~~!'

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

相关文章

解决python明明pip安装成功却找不到包的问题

如下所示: 原因1:版本不对,如用环境变量设置的python3.7路径,那么用的就是3.7的pip.exe安装了包。却用的是2.7的python运行 原因2:名称重复,在当前路径下有与i...

python迭代器常见用法实例分析

本文实例讲述了python迭代器常见用法。分享给大家供大家参考,具体如下: 迭代器 迭代是访问集合元素的一种方式。迭代器是一个可以记住遍历的位置的对象。迭代器对象从集合的第一个元素开始访...

Pyramid Mako模板引入helper对象的步骤方法

原理是我们在pyramind的before render event 中插入我们的helper 1. 创建helper.py文件,在里面添加上我们常用的方法 2. 在__init__.p...

python列表的常用操作方法小结

本文实例为大家了Python中列表(List)的详解操作方法,供大家参考,具体内容如下 1.创建列表。只要把逗号分隔的不同的数据项使用方括号括起来即可 List = ['wade','j...

python模块hashlib(加密服务)知识点讲解

官方文案:https://docs.python.org/zh-cn/3/library/hashlib.html hashlib --- 安全哈希与消息摘要 Python的hashl...