Using Python to shut down Windows scripts regularly

Source: Internet
Author: User
Because I often use laptops to share WiFi, but do not want to start a laptop night (for low-carbon environmental protection well ~_~!), so every time with the use of DOS command shutdown, feel good trouble. Just recently, I was learning Python and decided to write a timed shutdown script with Python:

Not much to say because the code is relatively simple, directly on the code.

code block


#-*-Coding:utf-8-*-"" "Created on Sat Dec 11:18:16 2015@author:win7" "" "" "" "" Script function: Under Windows, users enter the shutdown time in a certain format, the system  Automatically turn off the idea at a specified time: To get the specified time from the user input, minus the current time, and finally calculate the current time distance and how many seconds to use as the time parameter of the shutdown command. The system command that the OS uses to execute the set Command used to get system time: shutdown-s-t xxx where xxx is the number of seconds to auto power off, i.e. time parameter shutdown-a cancel shutdown schedule ' Import os,time# get user specified shutdown time PR int u ' instructions: Enter the shutdown time, format such as: Hours: minutes to get a chestnut: 20:21 and then hit enter. If you want to cancel the timer shutdown again, double-click Open Program input off to hit enter. Encode (' MBCS ') #u ' xxx '. Encode (' MBCS ')  The text character is displayed correctly in the console input_time=raw_input (U ' Please enter shutdown time, format such as: Hours: minutes: '. Encode (' MBCS ')) #取消定时关机 # There's always a change in plan if input_time = = ' off ': Os.system (' shutdown-a ') #输入数据检查 # because it is for self-use temporarily skip # Extract time division seconds h1 = Int (Input_time[0:2]) m1 = Int (Input_time[3:5]) #print h1,m1# Verify that getting the correct # gets the current system time MyTime = Time.strftime ('%h:%m:%s ') h2 = Int (Mytime[0:2]) m2 = int (mytime[3:5]) #print h2,m2 #验证获取是否正确 # Organize user input data to prevent the occurrence of 25:76:66 such time data if h1 > 24:h1 = m2 = 0if M1 > 60:m1 = 60if h1

The author says

Just started to learn Python soon, this script is relatively humble, a lot of features are not added to, such as: input data inspection, processing output data means also more rough, there are a lot of bad places to write, hope to see the great God can not hesitate to correct.

Problems with completing scripts

It was a bit of a blush to say, and a lot of low-level mistakes were made in writing the script. In order to be able to blush less later, but also to the newly-introduced small partners to provide a wrong teaching demonstration, so that they are recorded, the great God saw it as a joy bar! ~_~

1. Forget integers/integers = integers It's always wrong when you're testing, because when I convert the minutes I get to the hour, the number is 60, which is an integer, so the results are all wrong. And then I found out that I was drunk when I tested the results one after the other.

2. Forget the data type conversion raw_input () The resulting data is a string when I test it, I get a direct error and I think I'm drunk.

3. Finally, the character display problem begins when I finish writing the script when the console displays garbled and then after Baidu found the solution u ' xxx '. Encode (' MBCS ')

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.