网络编程
位置:首页>> 网络编程>> Python编程>> Windows自动执行python脚本操作步骤

Windows自动执行python脚本操作步骤

作者:i see the future  发布时间:2023-04-22 15:32:42 

标签:Windows,自动执行,python

上一篇自动在Windows中运行Python脚本并定时触发功能实现传送门链接

运行Python脚本:.bat文件

在Windows中,.bat文件是批处理文件,是与Linux中.sh(shell)文件很像的东西。
如果,我们想在Windows中运行一个Python脚本,我们可以通过CMD,首先进入python文件所在的目录,之后运行。
但是这样很麻烦,每次都要打开CMD,进入文件夹,运行文件。
所以,我们为了不每次都重复输入,建议把这些代码统一写在一个txt文件中,写完之后只要把txt文件的后缀改为.bat,然后双击运行就行啦。


cd C:\Users\Season\Desktop\
python timer.py

python脚本


import threading
import datetime
import time
def hello(RUNTIME):
   print ("执行一次...")
   global timer
   timer = threading.Timer(RUNTIME, hello,[RUNTIME,])
   timer.start()
if __name__ == "__main__":
   print("now {}".format(datetime.datetime.now()))
   year = int(time.strftime('%Y', time.localtime()))
   month = int(time.strftime('%m', time.localtime()))
   day = int(time.strftime('%d', time.localtime()))+1
   hour = int(time.strftime('%H', time.localtime()))
   minute = int(time.strftime('%M', time.localtime()))
   myruntime = datetime.datetime(year,month,day,hour,minute,0)
   RUNTIME = 24*60*60
   print("{} 运行".format(myruntime))
   timer = threading.Timer(RUNTIME, hello,[RUNTIME,])
   timer.start()

bat运行经验

运行第一次失败,原因是路径中文无法识别

Windows自动执行python脚本操作步骤

将路径更改为纯英文后,运行第二次成功。

Windows自动执行python脚本操作步骤

提示:建议使用绝对路径,并且路径为纯英文。

定时在Windows中触发.bat文件

在Windows中,依照如下步骤触发作业:

1.右键单击“我的电脑”

2.选择“Manage/管理”,弹出如下窗口

Windows自动执行python脚本操作步骤

3.依次选择System Tools/系统工具 - Task Scheduler/任务计划程序 - Task Scheduler Library/任务计划程序库

Windows自动执行python脚本操作步骤

4.在右边Action一栏点击“Create Basic Task/创建一个基本任务”,创建一个基本任务

5.填写任务名称与描述,随便写就好了。单击下一步。

Windows自动执行python脚本操作步骤

6.选择任务进行的频率与具体的时间

Windows自动执行python脚本操作步骤

7.操作为启动程序

Windows自动执行python脚本操作步骤

8.点击“Browse…”,选择我们想要运行的.bat文件

Windows自动执行python脚本操作步骤

9.然后下一步,就Finish了。

10.返回“任务计划程序库”,查看你的任务。

Windows自动执行python脚本操作步骤

来源:https://blog.csdn.net/m0_46629123/article/details/120070320

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com