Use python to write an windows timer shutdown script of recommends

  • 2020-05-27 06:00:39
  • OfStack

I often use my laptop to share WiFi, but I don't want to boot my laptop for 1 night. , so every time to use the command DOS shutdown, feel very troublesome. I happened to be learning Python recently, so I decided to use python to write a script for regular shutdown:

Say no more because the code is relatively simple, directly on the code.

The code block


# -*- coding: utf-8 -*-
"""
Created on Sat Dec 19 11:18:16 2015
@author: win7
"""
''' Timing shutdown '''
''' Script functions: windows Next, the user follows 1 Enter the shutdown time in the specified format, and the system will automatically shut down at the specified time 
   Idea: get the specified time from user input   The current time is subtracted from the time   The final calculation results in the current time distance specified 
   How many seconds are left   As a time parameter for the shutdown command 
'''
''' Modules to be used: 
        os  Used to execute configured system commands 
        time  Used to obtain system time 
  Commands to use:  shutdown -s -t xxx  Among them xxx Is the number of seconds taken for the distance from the automatic shutdown, that is, the time parameter       
        shutdown -a  Cancel shutdown plan 
'''
import os,time
# Gets a user-specified shutdown time 
print u' Instructions: enter the shutdown time, format such as: hours : minutes   Take a chestnut: 20:21  Then hit enter   Can be   
 If you want to cancel the scheduled shutdown   Double-click to open the program again   The input  off  Press enter   Can be '.encode('mbcs')
#u'xxx'.encode('mbcs')  Causes the text characters to appear correctly in the console 
input_time=raw_input(u' Please enter the shutdown time in the format of hours : minutes   : '.encode('mbcs'))
# Cancel scheduled shutdown 
# Plans are always changing   Leave the door open 
if input_time == 'off':
  os.system('shutdown -a')
# Input data check 
# For personal use   Skip the temporarily 
# Extraction time is minute and second 
h1 = int(input_time[0:2])
m1 = int(input_time[3:5])
#print h1,m1# Verify that the fetch is 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 # Verify that the fetch is correct 
# Collate user input data   To prevent 25:76:66 Time data like this 
if h1 > 24:
  h1 = 24
  m2 = 0
if m1 > 60:
  m1 = 60
if h1<h2:
  h1 = h1 + 24  
# Calculate number of seconds 
s1=(h1+(m1/60.0)-h2-(m2/60.0))*3600
print ' Before the shutdown  %d  seconds ' %s1
os.system('shutdown -s -t %d' %s1 ) 

The author says

Soon after I started to learn python by myself, this script was written in a simple way, with many functions not added, such as checking the input data, handling the output data in a rough way, and a lot of bad writing. I hope you can correct me.

Problems with completing the script

I'm a little embarrassed to say that I made a lot of silly mistakes in the process of writing the script. In order to be less blushing in the future, and to provide a teaching demonstration of a mistake for the beginner, I hereby record them down. ~ _ ~

1. I forgot the whole number/whole number = whole number. That's why the time was always wrong in the test because when I converted the minutes I got into hours, the division was 60

2. Forgot to convert raw_input () to get a string of data

3. Finally, the problem of character display began. When I finished writing the script and ran, what the console displayed was scrambled code.


Related articles: