Regular Python script running under Windows 7 environment (2020.10.21)

1. Writing a Python program (PrintTime.py)

        The function of this Python program is to create two notepad files Readme1.txt and Readme2.txt on the desktop, and write the start time and end time of the program to the file.

#-*- coding: UTF-8 -*-
import time
import os
def process(i):
    file_name = "C:\\Users\\Administrator\\Desktop\\Readme"+str(i)+".txt"
    file_write_obj = open(file_name,'w')
    Starttime = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
    print(Starttime)
    file_write_obj.writelines("程序运行开始时间"+Starttime+"\n")
    Endtime = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
    print(Endtime)
    file_write_obj.writelines("程序运行结束时间"+Endtime+"\n")
    file_write_obj.close()
if __name__ == '__main__':
    for i in range(1,3):
        process(i)

        Make sure that this program can run successfully, and the results are as follows:
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

2. Create a batch script (PrintTime.bat)

        In the directory where the PrintTime.py file is located, create a new PrintTime.txt, open the txt file, and enter the following code into it:

@echo off
e:
cd E:\计算智能代码\pythonProject
python PrintTime.py

        There are PrintTime.py and PrintTime.bat files in the file directory, as shown in the figure below:
Insert picture description here

3. Use Window's built-in task scheduler to run scripts regularly (PrintTime.bat)

        The first step is to open the Window task scheduler. The
Insert picture description here
         second step , in the task scheduler window, click Create task on the right side, and enter the task name PrintTime in the Create task window. The
Insert picture description here
Insert picture description here
         third step is to create a new trigger and set the starter's cycle (times). , Day, week, month) and start time.
Insert picture description here
Insert picture description here

        The fourth step is to create a new operation, select the previous PrintTime.bat script file in the program or script column of the new operation window, and fill in the path of the script file in the starting column: * E:\Compute Smart Code\pythonProject*
Insert picture description here
Insert picture description here
        The fifth step , in the task plan window interface, click on the task plan library, in the middle window to view the created task PrintTime, now all the steps are completed.
Insert picture description here

Fourth, the task's final timing execution results


Insert picture description here
        The contents of the two txt files,         Readme1.txt and Readme2.txt, are newly created on the system desktop as shown in the figure below.
Insert picture description here

Guess you like

Origin blog.csdn.net/jing_zhong/article/details/109201282