当前位置: 移动技术网 > IT编程>脚本编程>Python > python实现Windows电脑定时关机

python实现Windows电脑定时关机

2018年08月18日  | 移动技术网IT编程  | 我要评论

一株紫丁香教案,九九卡盟,育儿

定时关机,功能:windows下,用户按照一定格式输入关机时间,系统到指定时间自动关闭 思路:从用户输入获取指定时间 分别以时分秒减去当前时间 最终计算得到当前时间距离指定 时间还有多少秒 作为关机命令的时间参数。

需要用到的模块: os 用于执行设定的系统命令 time 用于获取系统时间 需要用到cmd命令: shutdown -s -t xxx 其中xxx为距离自动关机所用秒数,即时间参数 shutdown -a 取消关机计划。

代码:

import os
import time
input_time = input('请输入关机时间,格式如:小时:分钟 :')

if input_time == 'off':
  os.system('shutdown -a')

h1 = int(input_time[0:2])
m1 = int(input_time[3:5])

print(h1, m1)
mytime = time.strftime('%h:%m:%s')
h2 = int(mytime[0:2])
m2 = int(mytime[3:5])

if h1 > 24:
  h1 = 24
  m2 = 0
if m1 > 60:
  m1 = 60
if h1 < h2:
  h1 = h1 + 24

s1 = (h1+(m1/60.0)-h2-(m2/60.0))*3600
if s1 <= 0:
  print("error")
else:
  print('距离关机还有 %d 秒' %s1)
  os.system('shutdown -s -t %d' %s1)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网