python : windows : 子程序在新命令 window 和 运行 中打开一个 .bat 文件

python : windows : Subporcess open a .bat file in a new command window and run

我有一个 bat 文件,其中 运行 是一个 python 文件

test.bat

python test.py
pause

python 代码文件是

test.py

print("Hello World")

现在我想使用 suprocess 并打开 windows 命令提示符和 运行 .bat 文件

我尝试用子进程

创建一个python脚本

test2.py

import subprocess
filepath=r"D:\test.bat"
subprocess.Popen([filepath], shell=True) 

现在我尝试在命令提示符中 运行 这个 python 文件并期待另一个 windows 命令提示符打开和 运行 test.py 并暂停,我得到的是它没有打开,但是 运行 那里有 bat 文件本身

Microsoft Windows [Version 10.0.18363.1198]                                                                                                                     
(c) 2019 Microsoft Corporation. All rights reserved.                                                                                                            
                                                                                                                                                                
D:>python test2.py

D:>                                                                                                                                     
D:>python test.py                                                                                                                       
Hello World                                                                                                                                                     
                                                                                                                                                                
D:>pause                                                                                                                                
Press any key to continue . . . 

尝试在新的命令解释器中启动命令:

filepath=r"cmd /K D:\test.bat"

您可以使用 Windows 的 start 命令,它会启动一个单独的命令提示符 window 到 运行 指定的程序或命令:

import subprocess
filepath=r"D:\test.bat"
subprocess.Popen("start {}".format(filepath), shell=True)