Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler

 

在本教程中,我将向您展示使用Windows Scheduler安排 Python 脚本的步骤。当您想在规定的时间运行 Python 脚本时,这种类型的练习特别有用。

为了向您展示该过程是如何工作的,我将使用一个显示“Hello World!”的简单示例。每天早上 6 点。

希望不会太早……

 

使用 Windows 调度程序调度 Python 脚本的步骤

 

第 1 步:准备 Python 脚本

在我们的示例中,我将使用tkinter 模块来显示“Hello World!”的标签。

或者,您可以使用任何您想要安排的 Python 脚本。

这是我使用的 Python 脚本:

import tkinter as tk 

root= tk.Tk() 
 
canvas1 = tk.Canvas(root, width = 300, height = 300)
canvas1.pack()

label1 = tk.Label(root, text='Hello World!')
canvas1.create_window(150, 150, window=label1)

root.mainloop()

 

步骤 2:保存 Python 脚本

完成脚本编写后,将其保存为 Python 文件(文件类型应为.py):

Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler

在我的例子中,我将 Python 脚本保存在我的桌面上,在这个路径下:

C:\Users\Ron\Desktop\Hello_World.py

 

步骤 3:创建批处理文件以运行 Python 脚本

接下来,您需要创建一个批处理文件来运行 Python 脚本。

首先,打开记事本,然后应用以下通用结构:

"Path where your Python exe is stored\python.exe" "Path where your Python script is stored\script name.py"
pause

 

就我而言:

  • 我的 Python exe 存储路径\python.exe 是:
    “C:\Users\Ron\AppData\Local\Programs\Python\Python37-32\python.exe”
  • 我的 Python 脚本的存储路径\script name.py 是(参见步骤 2):
    “C:\Users\Ron\Desktop\Hello_World.py”

您需要调整这些路径以适合您的实例。

 

以下是添加路径后我的 记事本 的外观:

Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler
Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler

 

最后,使用您的文件名和“bat”扩展名保存记事本:

文件名.bat

 

例如,我决定将桌面上的记事本保存为:

Run_Python_Script.bat

Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler
Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler

 

保存记事本后,在我的桌面上创建了一个新的批处理文件(称为 Run_Python_Script):

Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler

此批处理文件将在双击它时运行 Python 脚本:

Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler
Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler

 

在下面的最后一步中,您将看到如何使用 Windows 调度程序调度该批处理文件以执行 Python 脚本。

 

第 4 步:使用 Windows 调度程序调度 Python 脚本

对于这一步,我将使用 Windows 10 通过 Windows 调度程序执行 Python 脚本。使用以前版本的 Windows 时也适用类似的原则。

首先,打开控制面板,然后单击 管理工具

Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler

接下来,双击任务计划程序,然后选择“创建基本任务…”选项

Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler
Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler

 

为您的任务键入一个名称(如果需要,您也可以键入描述),然后按下一步。

在这里,我将任务命名为:Run Hello World

Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler
Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler

 

接下来,我选择启动“每日”任务,因为我们希望每天早上 6 点运行 Python 脚本:

Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler
Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler

 

从 2019 年 4 月 1 日开始,该动作将在每天早上 6 点再次发生。您可以调整这些时序参数以满足您的需要。

Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler
Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler

 

选择Start a program,然后按 Next:

Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler
Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler

 

接下来,使用浏览按钮查找运行 Python 脚本的批处理文件。就我而言,我将Run_Python_Script批处理文件放在我的桌面上:

Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler
Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler

 

最后,单击Finish,您应该一切顺利:

Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler
Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler

 

从此时起,您将收到“Hello World!”的问候。每天早上 6 点:

Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler

请注意,使用 Windows 调度程序执行 Python 脚本的方法不止一种。我选择创建一个批处理文件来运行 Python 脚本。

这对于不熟悉 Python 但想在不启动 Python 应用程序的情况下运行脚本的用户特别有用。在任何给定的时间点,除了规定的时间,用户只需双击批处理文件并从 Python 获取输出。

最后,您希望访问 任务调度程序文档以了解有关调度任务的更多信息。

 

 

本文:Windows 定时任务管理, Windows 定时执行程序, Windows 调用 Python 脚本, How to Schedule Python Script using Windows Scheduler

 

Loading

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.