如何一次执行多个.py文件

一、使用批处理文件(.bat)

  1. 打开记事本或任何文本编辑器。
  2. 编写批处理命令来调用每个Python脚本。确保使用python命令(或者Python解释器的完整路径,如果python没有添加到PATH环境变量中)。
  3. 将文件保存为.bat扩展名,比如run_py_scripts.bat。
  4. 双击执行你创建的.bat文件。

你已经将Python安装路径添加到了系统的PATH变量中

@echo off  
echo Executing Python scripts...  
  
python D:\……\test1.py  
python D:\……\test2.py  
python D:\……\test3.py  
  
echo All scripts have been executed.  
pause

如果你没有将Python添加到PATH变量中,你需要使用Python解释器的完整路径,比如:

@echo off  
echo Executing Python scripts...  
  
"C:\Program Files\Python311\python.exe" D:\yourPath\test1.py  
"C:\Program Files\Python311\python.exe" D:\yourPath\test2.py  
"C:\Program Files\Python311\python.exe" D:\yourPath\test3.py  
  
echo All scripts have been executed.  
pause

同时并行执行多个.py文件

@echo off  
echo Executing Python scripts...  
  
start python D:\……\test1.py  
start python D:\……\test2.py  
start python D:\……\test3.py  
  
echo All scripts have been executed.  
pause

···

···



# 二、使用 Python 脚本调用其他脚本
1. 你也可以编写一个 Python 脚本,该脚本使用 subprocess 模块或其他方法来调用其他 Python 脚本。
2. 然后,你可以在 VS Code 中运行这个 runner.py 脚本。

runner.py

import subprocess

scripts = [
r’D:\yourPath\test1.py’,
r’D:\yourPath\test2.py’,
r’D:\yourPath\test3.py’,
]

for script in scripts:
subprocess.run([‘python’, script], check=True)

print(“All scripts have been executed.”)

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值