在一个Windows命令提示符中按顺序运行多个程序?

Sea*_*ean 6 python eclipse windows console subprocess

我需要一个接一个地运行多个程序,并且每个程序都在控制台窗口中运行.我希望控制台窗口可见,但是为每个程序创建了一个新窗口.这很烦人,因为每个窗口都是在另一个关闭的新位置打开,并且在Eclipse中工作时窃取焦点.

这是我使用的初始代码:

def runCommand( self, cmd, instream=None, outstream=None, errstream=None ):
    proc = subprocess.Popen( cmd, stdin=instream, stdout=outstream, stderr=errstream )

    while True:
        retcode = proc.poll()
        if retcode == None:
            if mAbortBuild:
                proc.terminate()
                return False
            else:
                time.sleep(1)
        else:
            if retcode == 0:
                return True
            else:
                return False
Run Code Online (Sandbox Code Playgroud)

我在调用subprocess.Popen然后调用proc.stdin.write(b'program.exe\r \n')时切换到使用'cmd'打开命令提示符.这似乎解决了一个命令窗口问题,但现在我无法判断第一个程序何时完成,我可以启动第二个程序.我想在运行第二个程序之前停止并查询第一个程序中的日志文件.

有关如何实现这一目标的任何提示?是否有另一个选项在一个窗口中运行我尚未找到?

mar*_*eau 6

由于您使用的是Windows,因此您只需创建一个批处理文件,列出您要运行的每个程序,这些程序将在一个控制台窗口中执行.因为它是一个批处理脚本,所以你可以在其中放置条件语句,如示例所示.

import os
import subprocess
import textwrap

# create a batch file with some commands in it
batch_filename = 'commands.bat'
with open(batch_filename, "wt") as batchfile:
    batchfile.write(textwrap.dedent("""
        python hello.py
        if errorlevel 1 (
            @echo non-zero exit code: %errorlevel% - terminating
            exit
        )
        time /t
        date /t
    """))

# execute the batch file as a separate process and echo its output
kwargs = dict(stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
              universal_newlines=True)
with subprocess.Popen(batch_filename, **kwargs).stdout as output:
    for line in output:
        print line,

try: os.remove(batch_filename)  # clean up
except os.error: pass
Run Code Online (Sandbox Code Playgroud)