基本信息
源码名称:python 实现定时关机(windows,python3)
源码大小:2.47KB
文件格式:.py
开发语言:Python
更新时间:2019-05-23
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

import os
import time
from datetime import datetime


def close_job1(times):
    print(
        '现在是',
        time.strftime(
            '%Y-%m-%d %H:%M:%S',
            time.localtime(
                time.time())))
    now = str(datetime.now()).rpartition(":")[0].split(" ")[1]
    now_tosec = (int(now.split(":")[0]) * 60   int(now.split(":")[1])) * 60
    t_tosec = (int(times.split(":")[0]) * 60   int(times.split(":")[1])) * 60
    diff = int((t_tosec - now_tosec) / 60)
    print("定时任务启动,系统将在 {},即{}分钟后 自动关闭" .format(times, diff))
    command = "shutdown -s -t {}".format(diff * 60)
    os.system(command)


def close_job2(times):
    print(
        '现在是',
        time.strftime(
            '%Y-%m-%d %H:%M:%S',
            time.localtime(
                time.time())))
    now = str(datetime.now()).rpartition(":")[0].split(" ")[1]
    sum = int(now.split(":")[1])   int(times)
    if sum >= 60:
        a = sum // 60
        b = str(sum % 60)
        if len(b) < 2:
            b = "0"   b
            tim = str(int(now.split(":")[0])   a)   ":"   b
            print("定时任务启动,系统将在 {},即{}分钟后 自动关闭" .format(tim, times))
        else:
            tim = str(int(now.split(":")[0])   a)   ":"   b
            print("定时任务启动,系统将在 {},即{}分钟后 自动关闭".format(tim, times))
    else:
        tim = str(int(now.split(":")[0]))   ":"   str(sum)
        print("定时任务启动,系统将在 {},即{}分钟后 自动关闭" .format(tim, times))
    times = times * 60
    command = "shutdown -s -t {}".format(times)
    os.system(command)


def cancel_job():
    print(
        '现在是',
        time.strftime(
            '%Y-%m-%d %H:%M:%S',
            time.localtime(
                time.time())))
    os.system("shutdown -a")
    print("定时关机任务已取消")


if __name__ == '__main__':
    mode = int(input('选择模式\n1、指定时间关闭\n2、XX分钟后关闭\n3、取消定时关闭任务\n'))
    if mode == 1:
        close_time = input("输入关闭系统||时间格式如|21:56|\n\t")
        close_job1(close_time)
    elif mode == 2:
        close_time = int(input("输入分钟数进行关闭系统||时间格式如|60|\n\t"))
        close_job2(close_time)
    elif mode == 3:
        cancel_job()
    else:
        print("输入有误,默认取消定时关机")
        cancel_job()