Home Backend Development Python Tutorial Using python scheduled shutdown windows script

Using python scheduled shutdown windows script

Mar 22, 2017 am 09:14 AM
python windows Scheduled 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 )
Copy after login

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 of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1230
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

Golang vs. Python: Key Differences and Similarities Golang vs. Python: Key Differences and Similarities Apr 17, 2025 am 12:15 AM

Golang and Python each have their own advantages: Golang is suitable for high performance and concurrent programming, while Python is suitable for data science and web development. Golang is known for its concurrency model and efficient performance, while Python is known for its concise syntax and rich library ecosystem.

How to solve complex BelongsToThrough relationship problem in Laravel? Use Composer! How to solve complex BelongsToThrough relationship problem in Laravel? Use Composer! Apr 17, 2025 pm 09:54 PM

In Laravel development, dealing with complex model relationships has always been a challenge, especially when it comes to multi-level BelongsToThrough relationships. Recently, I encountered this problem in a project dealing with a multi-level model relationship, where traditional HasManyThrough relationships fail to meet the needs, resulting in data queries becoming complex and inefficient. After some exploration, I found the library staudenmeir/belongs-to-through, which easily installed and solved my troubles through Composer.

See all articles