在没有外壳风的情况下运行shell命令

2024-06-12 21:35:39 发布

您现在位置:Python中文网/ 问答频道 /正文

使用subprocess.callsubprocess.Popen,执行shell命令会使shell窗口快速出现和消失。在

如果没有shell窗口,如何运行shell命令?在


Tags: 命令shellcallsubprocesspopen消失
1条回答
网友
1楼 · 发布于 2024-06-12 21:35:39

我想你的观察仅限于Windows,因为我相信,这是唯一一个你会遇到“控制台闪存”问题的平台。如果是这样,那么docs提供以下半有用的段落:

The startupinfo and creationflags, if given, will be passed to the underlying CreateProcess() function. They can specify things such as appearance of the main window and priority for the new process. (Windows only)

不幸的是,Python联机文档没有复制Windows API文档的相关部分,因此您必须在其他地方找到这些文档,例如,在MSDN上启动here,这会导致您here来获得{},特别是

CREATE_NO_WINDOW
0x08000000

The process is a console application that is being run without a console window. Therefore, the console handle for the application is not set.

因此,将creationflags=0x08000000添加到您的Popen调用中应该会有所帮助(不幸的是,我没有运行Windows的机器来尝试这一点,所以您必须亲自尝试)。在

相关问题 更多 >