简体   繁体   English

Python:在 Windows 上启动新的命令提示符并等待它完成/退出

[英]Python: Start new command prompt on Windows and wait for it finish/exit

I don't understand why it's so hard to do this on Windows.我不明白为什么在 Windows 上执行此操作如此困难。

I want to spawn a bunch of command prompt windows which will run other scripts.我想生成一堆将运行其他脚本的命令提示符窗口。 The reason I want this is so I can see all the output from each script neatly (if I have them just be threads/subprocesses in the main window I can't view all the output properly).我想要这个的原因是我可以整齐地看到每个脚本的所有输出(如果我在主窗口中只有线程/子进程,我无法正确查看所有输出)。 I also don't want to log the output because it's mostly for viewing progress bars, which don't really work with log files.我也不想记录输出,因为它主要用于查看进度条,而这实际上不适用于日志文件。

So individual parts of my requirements work, but not together:所以我的要求的各个部分都可以工作,但不能一起工作:

os.system("start cmd /c {command here}")     # Launches in new command prompt, closes when done

However, os system won't let me wait until the command finishes (since start is the actual command, the second it opens the new command prompt it's "done")但是,操作系统不会让我等到命令完成(因为开始是实际命令,第二个它打开新的命令提示符是“完成”)

Similarly if I try:同样,如果我尝试:

p = subprocess.Popen(["start", "cmd", "/k", "{command here}"], shell = True) # Needs to be shell since start isn't an executable, its a shell cmd
p.wait()    # I can wait until finished (although it too finishes after start finishes)

So how do I do this?那么我该怎么做呢? I read somewhere that a solution could be to use processgroup but it's unix only....or something like that我在某处读到一个解决方案可能是使用 processgroup 但它只是 unix ....或类似的东西

Or if you have a neat way of displaying the output from all the subprocesses in a single window, then I don't need to open a new command prompt and can simply use threads.或者,如果您有一种巧妙的方式在单个窗口中显示所有子进程的输出,那么我不需要打开新的命令提示符,只需使用线程即可。 That works too, but if I have lets say 4 threads downloading something and displaying a progress bar as well as outputting other information I don't know how to display that in a way that can be read (as well as avoiding them all colliding with each other).这也行得通,但是如果我让说 4 个线程下载一些东西并显示进度条以及输出其他信息,我不知道如何以一种可以读取的方式显示它(以及避免它们全部与彼此)。

PS: This is on Windows Vista. PS:这是在 Windows Vista 上。 PPS: I'd preferably like a solution that works on Windows, Linux and Mac, I'm focusing on Windows for now but I'd like a solution that works for all three, and I know Windows is the most finicky. PPS:我更喜欢一个适用于 Windows、Linux 和 Mac 的解决方案,我现在专注于 Windows,但我想要一个适用于所有这三者的解决方案,而且我知道 Windows 是最挑剔的。 I would just substitute "the start cmd /c" for the OS appropriate command.我只是用“start cmd /c”代替操作系统适当的命令。

Upon reading your comment to my previous answer what you need is:阅读您对我之前的回答的评论后,您需要的是:

os.system("start /wait cmd /c {command}")

Keep the windows command reference always at hand!Windows 命令参考放在手边!

The accepted answer didn't work for me.接受的答案对我不起作用。
To open on a new command prompt I had to use:要在新的命令提示符下打开,我必须使用:

os.system("start /B start cmd.exe @cmd /k mycommand...")

For me this seems to work对我来说这似乎有效
os.system("cmd /k {command}")

With /k cmd executes and then remain open使用/k cmd 执行然后保持打开状态
With /c executes and close使用/c执行并关闭

To open a new command window and then execute the command打开一个新的命令窗口,然后执行命令
os.system("start cmd /k {command}")

You can pass /WAIT to the start command to make it wait for termination.您可以将 /WAIT 传递给start命令以使其等待终止。

How about怎么样

os.system("cmd /c {command here}") 

Or even甚至

os.system("{command here}")

It is the start command the one that is launching a separate session instead of using the same one the python program is running on.它是开始命令,它启动一个单独的会话,而不是使用运行 python 程序的同一个会话。

The simplest way (as pointed out https://stackoverflow.com/a/11615580/3312367 ) to open a new cmd-window is to use打开新的 cmd-window 的最简单方法(如https://stackoverflow.com/a/11615580/3312367所指出的)是使用

os.system("start /wait cmd /c {command}")

but it's not very useful if your command is a complex one with spaces or other control characters.但如果您的命令是带有空格或其他控制字符的复杂命令,则它不是很有用。 For that I use:为此,我使用:

subprocess.run(["start", "/wait", "cmd", "/K", command, "arg /?\^"], shell=True)

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

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