search
HomeBackend DevelopmentPython TutorialUsing python scheduled shutdown windows script
Using python scheduled shutdown windows scriptMar 22, 2017 am 09:14 AM
pythonwindowsScheduled shutdown

Since I often use my laptop to share WiFi, but I don’t want my laptop to be turned on overnight (for the sake of low carbon and environmental protection ~_~!), so I have to use DOS commands to shut it down every time, which feels very troublesome. I happened to be learning Python recently, so I decided to use python to write a scheduled shutdown script:

Not much to say since the code is relatively simple, let’s go directly to the code.

Code block


# -*- coding: utf-8 -*-
"""
Created on Sat Dec 19 11:18:16 2015
@author: win7
"""
'''定时关机'''
'''脚本功能:windows下,用户按照一定格式输入关机时间,系统到指定时间自动关闭
  思路:从用户输入获取指定时间 分别以时分秒减去当前时间 最终计算得到当前时间距离指定
  时间还有多少秒 作为关机命令的时间参数
'''
'''需要用到的模块:
        os 用于执行设定的系统命令
        time 用于获取系统时间
 需要用到的命令: shutdown -s -t xxx 其中xxx为距离自动关机所用秒数,即时间参数      
        shutdown -a 取消关机计划
'''
import os,time
#获取用户指定关机时间
print u'使用说明:输入关机时间,格式如:小时:分钟 举个栗子:20:21 然后敲回车 即可  
如果想取消定时关机 再次双击打开程序 输入 off 敲回车 即可'.encode('mbcs')
#u'xxx'.encode('mbcs') 使正文字符在控制台正确显示
input_time=raw_input(u'请输入关机时间,格式如:小时:分钟 :'.encode('mbcs'))
#取消定时关机
#计划总有变化 先留条后路
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])
#print h2,m2 #验证获取是否正确
#对用户输入数据进行整理 防止出现25:76:66这样的时间数据
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
print &#39;距离关机还有 %d 秒&#39; %s1
os.system(&#39;shutdown -s -t %d&#39; %s1 )

The author said

Not long after I started learning python by myself, this script was relatively crude. , many functions have not been added, such as: checking of input data, the method of processing output data is also relatively rough, and there are many poorly written places. I hope that the masters who see it can correct me.

Problems encountered in completing the script

It feels a little embarrassing to say that I made a lot of low-level mistakes in the process of writing the script. In order to avoid blushing in the future and to provide a teaching example of errors for those who are just getting started, I hereby record them all. It will be a joy for the master to read them! ~_~

1. I forgot about integer/integer = integer. The time was always wrong during the test because when I converted the minutes into hours, the number I divided was 60. This is an integer, so the results I got were all wrong. Later, I calculated the results one by one. During the output test, I realized that I was drunk

2. I forgot to convert the data type and the data obtained by raw_input() was a string. When I tested, I reported an error directly and then I remembered that I was drunk

3. Finally, there was a problem with character display. When I finished writing the script and ran it, the console displayed garbled characters. Later, I found a solution through Baidu u'xxx'.encode('mbcs')

The above is the detailed content of Using python scheduled shutdown windows script. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

Python自动化实践之筛选简历Python自动化实践之筛选简历Jun 07, 2022 pm 06:59 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

分享10款高效的VSCode插件,总有一款能够惊艳到你!!分享10款高效的VSCode插件,总有一款能够惊艳到你!!Mar 09, 2021 am 10:15 AM

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

详细介绍python的numpy模块详细介绍python的numpy模块May 19, 2022 am 11:43 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

python中文是什么意思python中文是什么意思Jun 24, 2019 pm 02:22 PM

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.