通过CMD调用多个批处理文件不会阻止 [英] Call to several batch files through CMD doesn't block

查看:112
本文介绍了通过CMD调用多个批处理文件不会阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python槽CMD依次调用几个install.bat文件.

I'm trying to call several install.bat files one after another with Python trough CMD.

每个bat文件都必须显示在交互式控制台窗口中,因为它会询问一些用户指令,并且python程序仅在解决每个CMD进程后才能恢复 每个install.bat文件可能需要很长时间才能完成其过程.

It is necessary that each bat file be displayed in an interactive console window because it asks for some users instructions and that the python program only resume after each CMD process is resolved Each install.bat file can take a pretty long time to finish its process.

我的代码如下:

for game in games :
    print("----------- Starting conversion for %s -----------" %game)       
    subprocess.call("start cmd /C " + "Install.bat", cwd=os.path.join(gamesDosDir,game), shell=True)        

print("end")   

但是外壳中的控制台窗口是全部立即启动的,并且"end"消息在它们中的任何一个完成之前就出现了事件,而我希望它们一个一个地出现并且直到n + 1时才进入n + 1个窗口. n完成并关闭控制台窗口(然后由用户或自动/K或/C).

But the console windows inside the shell are launched all at once and the "end" message appears event before any of them is finished, whereas I would like them appearing one by one and not go to the n+1 one until the n one is finished and the console window closed (either by user or automatically /K or /C then).

我了解这是使用CMD的一些问题,因为通话应被阻止.怎么解决呢?另外,如果可能的话,如何使其保持完全相同,并在默认的用户输入中添加"Y"和"Y"?

I understand this is some problems using CMD as call should be blocking. How to resolve that? Additionally, if possible how to keep it exactly the same and add 'Y' and 'Y' as default user input?

推荐答案

如果将批处理文件作为参数传递给cmd /c,则是启动批处理文件(或更常见的CLI命令)的最常用方法.发表评论后,我可以假定您需要使用start来强制创建(新)命令窗口.

The most common way to start a batch file (or more generally a CLI command) if to pass it as an argument to cmd /c. After you comment I can assume that you need to use start to force the creation of a (new) command window.

在这种情况下,正确的方法是在启动命令中添加/wait选项:它将强制启动命令等待其子进程的结束:

In that case the correct way is to add the /wait option to the start command: it will force the start command to wait the end of its subprocess:

subprocess.call("start /W cmd /C " + "Install.bat", cwd=os.path.join(gamesDosDir,game),
    shell=True)

但是@eryksun提出了一种更清洁的方法.在Windows上,.bat文件可以在不使用shell = True的情况下执行,而creationflags=CREATE_NEW_CONSOLE足以确保创建新控制台.因此,上面的行可能会变成:

But @eryksun proposed a far cleaner way. On Windows, .bat files can be executed without shell = True, and creationflags=CREATE_NEW_CONSOLE is enough to ensure a new console is created. So above line could simply become:

subprocess.call("Install.bat", cwd=os.path.join(gamesDosDir,game),
    creationflags = subprocess.CREATE_NEW_CONSOLE)

这篇关于通过CMD调用多个批处理文件不会阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆