在不打开 Windows 命令提示符的情况下从 Python 执行子进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6390394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executing subprocess from Python without opening Windows Command Prompt
提问by Keenan Pepper
Possible Duplicate:
Running a process in pythonw with Popen without a console
How do I eliminate Windows consoles from spawned processes in Python (2.7)?
可能的重复:
在没有控制台的情况下使用 Popen 在 pythonw 中运行进程
如何从 Python (2.7) 中生成的进程中消除 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.
我有一个 Python 程序,它多次调用一个单独的数字处理程序(用 C 编写)作为子进程(使用 subprocess.check_call)。它在 Linux 上运行良好,在 Windows 上也运行良好,除了一件小事:每次它调用子进程时,都会创建一个命令提示符窗口,然后在子进程退出时很快将其销毁。
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.
这根本不影响计算,但是很烦人,因为这个窗口不断地在屏幕上闪烁,并且很难在计算机上做其他事情,因为新的命令提示符窗口可以窃取键盘焦点。
How can I simply execute the subprocess (which has no GUI), and prevent the Command Prompt window from being created?
如何简单地执行子进程(没有 GUI),并防止创建命令提示符窗口?
采纳答案by Bryan
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()
?如果通过,shell=True
则不应创建窗口,因为这将导致SW_HIDE
为STARTUPINFO.wShowWindow
属性设置标志。
Example:
例子:
subprocess.check_call(["ping", "google.com"], shell=True)