简体   繁体   English

python:windows:子进程在新命令 window 中打开 a.bat 文件并运行

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

I have a bat file in which it runs a python file我有一个 bat 文件,它在其中运行一个 python 文件

test.bat

python test.py
pause

the python code file is python 代码文件是

test.py

print("Hello World")

Now i want to use suprocess and open a windows command prompt and run the.bat file现在我想使用 suprocess 并打开 windows 命令提示符并运行 .bat 文件

i tried creating a python script with subprocess我尝试用子进程创建一个 python 脚本

test2.py

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

Now I try to run this python file in command Promp and expecting another windows command prompt to open and run the test.py and pause, what i get is it does not open, but runs the bat file there itself现在我尝试在命令提示符中运行这个 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 . . . 

Try to launch a command in a new command interpreter:尝试在新的命令解释器中启动命令:

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

You could make use of Windows's start command, which starts a separate Command Prompt window to run a specified program or command:您可以使用 Windows 的启动命令,它会启动一个单独的命令提示符 window 来运行指定的程序或命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM