由于使用的是Windows,因此您可以创建一个批处理文件,列出要运行的每个程序,这些程序都将在单个控制台窗口中执行。由于它是一个批处理脚本,因此您可以执行诸如在示例中放入条件语句之类的操作。
import osimport subprocessimport textwrap# create a batch file with some commands in itbatch_filename = 'commands.bat'with open(batch_filename, "wt") as batchfile: batchfile.write(textwrap.dedent(""" python hello.py if errorlevel 1 ( @echo non-zero exit pre: %errorlevel% - terminating exit ) time /t date /t """))# execute the batch file as a separate process and echo its outputkwargs = 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 upexcept os.error: pass


