Python:在 Windows 上启动新的命令提示符并等待它完成/退出 [英] Python: Start new command prompt on Windows and wait for it finish/exit

查看:123
本文介绍了Python:在 Windows 上启动新的命令提示符并等待它完成/退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么在 Windows 上执行此操作如此困难.

I don't understand why it's so hard to do this on 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

但是,操作系统不会让我等到命令完成(因为 start 是实际命令,第二次打开新命令提示符它就完成"了)

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")

同样,如果我尝试:

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)

那我该怎么做呢?我在某处读到一个解决方案可能是使用 processgroup 但它只是 unix ......或类似的东西

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

或者,如果您有一种简洁的方式在单个窗口中显示所有子进程的输出,那么我不需要打开新的命令提示符,只需使用线程即可.这也有效,但是如果我说 4 个线程下载某些内容并显示进度条以及输出其他信息,我不知道如何以可以读取的方式显示它(以及避免它们全部与彼此).

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).

PS:这是在 Windows Vista 上.PPS:我更喜欢适用于 Windows、Linux 和 Mac 的解决方案,我现在专注于 Windows,但我想要一个适用于所有三个的解决方案,而且我知道 Windows 是最挑剔的.我只是将启动 cmd/c"替换为操作系统相应的命令.

PS: This is on 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. I would just substitute "the start cmd /c" for the OS appropriate command.

推荐答案

阅读您对我之前回答的评论后,您需要的是:

Upon reading your comment to my previous answer what you need is:

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

保留 Windows 命令参考随时可用!

这篇关于Python:在 Windows 上启动新的命令提示符并等待它完成/退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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