从 Python 执行子进程而不打开 Windows 命令提示符
问题描述
可能重复:
在没有控制台的情况下使用 Popen 在 pythonw 中运行进程一个>
如何消除 Windows 控制台来自 Python (2.7) 中的衍生进程?
我有一个 Python 程序,它多次调用一个单独的数字处理程序(用 C 编写)作为子进程(使用 subprocess.check_call).它在 Linux 上运行良好,在 Windows 上也运行良好,除了一件小事:每次调用子进程时都会创建一个命令提示符窗口,然后在子进程退出时很快将其销毁.
I have a Python program that calls a separate number-crunching program (written in C) as a subprocess several times (using subprocess.check_call). It works great on Linux, and it works great on Windows too except for one little thing: every time it calls the subprocess a Command Prompt window is created and then soon destroyed when the subprocess exits.
这根本不影响计算,但是很烦人,因为这个窗口一直在屏幕上闪烁,并且在计算机上做其他事情变得困难,因为新的命令提示符窗口可以窃取键盘焦点.
This doesn't affect the computation at all, but it's very annoying because this window keeps flashing on and off the screen, and it makes it difficult to do other things on the computer because the new Command Prompt window can steal keyboard focus.
如何简单地执行子进程(没有 GUI),并防止创建命令提示符窗口?
How can I simply execute the subprocess (which has no GUI), and prevent the Command Prompt window from being created?
解决方案
你是怎么调用subprocess.check_call()
的?如果您传递 shell=True
则不应创建窗口,因为这将导致为 STARTUPINFO.wShowWindow
属性设置 SW_HIDE
标志.
How are you calling subprocess.check_call()
? If you pass shell=True
then the window should not be created as this will cause the SW_HIDE
flag to be set for the STARTUPINFO.wShowWindow
attribute.
例子:
subprocess.check_call(["ping", "google.com"], shell=True)
相关文章