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

yipeiwu_com6年前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中实现替换字符串中的子串的示例

假如有个任务: 给定一个字符串,通过查询字典,来替换给定字符中的变量。如果使用通常的方法: >>> "This is a %(var)s" % {"var":"do...

在Django框架中设置语言偏好的教程

一旦你准备好了翻译,如果希望在Django中使用,那么只需要激活这些翻译即可。 在这些功能背后,Django拥有一个灵活的模型来确定在安装和使用应用程序的过程中选择使用的语言。 要设定一...

在django中自定义字段Field详解

Django的Field类中方法有: to_python() # 把数据库数据转成python数据 from_db_value() # 把数据库数据转成python数据 get_pre_...

python getpass模块用法及实例详解

python getpass模块用法及实例详解

getpass import getpass username = input("username:") password = getpass.getpass("passwor...

Python3.x和Python2.x的区别介绍

1.性能Py3.0运行 pystone benchmark的速度比Py2.5慢30%。Guido认为Py3.0有极大的优化空间,在字符串和整形操作上可以取得很好的优化结果。Py3.1性能...