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 '距离关机还有 %d 秒' %s1 os.system('shutdown -s -t %d' %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!

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

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

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

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

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

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

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
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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.