从python执行程序,以便在单独的cmd.exe窗口中打开

从python执行程序,以便在单独的cmd.exe窗口中打开,python,cmd,subprocess,Python,Cmd,Subprocess,如何从python程序中执行程序,使其在单独的cmd.exe窗口中打开并输出已执行程序? 我尝试使用subprocess.popen,但在程序运行时它不会显示cmd.exe窗口。在Windows中,您需要声明可选变量shell=True并使用start: subprocess.Popen('start executable.exe', shell=True) 或者,如果要在运行可执行文件后终止shell: subprocess.Popen('start cmd /C executable.ex

如何从python程序中执行程序,使其在单独的cmd.exe窗口中打开并输出已执行程序?
我尝试使用subprocess.popen,但在程序运行时它不会显示cmd.exe窗口。

在Windows中,您需要声明可选变量shell=True并使用start:

subprocess.Popen('start executable.exe', shell=True)
或者,如果要在运行可执行文件后终止shell:

subprocess.Popen('start cmd /C executable.exe', shell=True)
例如:

subprocess.Popen('start dir', shell=True)

subprocess.Popen('start cmd /C dir', shell=True)

在Windows中,您需要声明可选变量shell=True并使用start:

subprocess.Popen('start executable.exe', shell=True)
或者,如果要在运行可执行文件后终止shell:

subprocess.Popen('start cmd /C executable.exe', shell=True)
例如:

subprocess.Popen('start dir', shell=True)

subprocess.Popen('start cmd /C dir', shell=True)

我尝试导入子进程,os cd=os.getcwd()p=subprocess.Popen([cd+“\\\”+'cmd.exe',cd+“\\”+'program.bat'],shell=True),但它没有在cmd.exe窗口中打开。请尝试在可执行文件之前添加start。使用
start
的问题是生成一个新进程,而不是等待它完成。也就是说,返回的Popen对象的
wait()
方法立即返回,并且无法直接知道进程何时完成。如果您希望在新控制台中启动流程,并且仍然能够正确地等待流程完成,请正常使用Popen(无“start”、“cmd”、shell=True),但只需传递额外的
creationflags=subprocess.CREATE_new_控制台
参数)。详细信息:我尝试导入子进程,os cd=os.getcwd()p=subprocess.Popen([cd+“\\”+'cmd.exe',cd+“\\”+'program.bat'],shell=True),但它没有在cmd.exe窗口中打开。尝试在可执行之前添加start使用
start
的问题是生成一个新进程,而不是等待它完成。也就是说,返回的Popen对象的
wait()
方法立即返回,并且无法直接知道进程何时完成。如果您希望在新控制台中启动流程,并且仍然能够正确地等待流程完成,请正常使用Popen(无“start”、“cmd”、shell=True),但只需传递额外的
creationflags=subprocess.CREATE_new_控制台
参数)。细节: