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的话需要判断执行命令。

相关文章

Pytorch根据layers的name冻结训练方式

使用model.named_parameters()可以轻松搞定, model.cuda() # ######################################...

使用C++扩展Python的功能详解

使用C++扩展Python的功能详解

本文主要研究的是使用C++扩展Python的功能的相关问题,具体如下。 环境 VS2005Python2.5.4Windows7(32位) 简介 长话短说,这里说的扩展Python功能与...

使用Python设置tmpfs来加速项目的教程

 对我当前工程进行全部测试需要花费不少时间。既然有 26 GB 空闲内存,为何不让其发挥余热呢? tmpfs 可以通过把文件系统保存在大内存中来加速测试的执行效率。 但...

使用Python的turtle模块画图的方法

使用Python的turtle模块画图的方法

简介:turtle是一个简单的绘图工具。它提供了一个海龟,你可以把它理解为一个机器人,只听得懂有限的指令。 1.在文件头写上如下行,这能让我们在语句中插入中文 #-*-coding:ut...

python导入时小括号大作用

在导入Python模块时,我们可以用 import os 也可以用 from os import * 当然,不推荐第二种方法,这样,会导入太多的os模块内的函数,所以...