在没有 shell 窗口的情况下运行 shell 命令

发布于 2024-09-03 06:59:31 字数 141 浏览 2 评论 0原文

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

如何在没有 shell 窗口的情况下运行 shell 命令?

With either subprocess.call or subprocess.Popen, executing a shell command makes a shell window quicky appear and disappear.

How can I run the shell command without the shell window?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一页 2024-09-10 06:59:31

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

启动信息和创建标志,如果
给定,将传递给
底层的 CreateProcess() 函数。
他们可以指定诸如
主窗口的外观和
新进程的优先级。 (视窗
仅)

不幸的是,Python 在线文档不会重现 Windows API 文档的相关部分,因此您必须在其他地方找到它们,例如从 此处位于 MSDN 上,它会引导您此处用于creationflags,特别是

CREATE_NO_WINDOW
0x08000000

该进程是一个控制台应用程序
在没有控制台的情况下运行
窗户。因此,控制台句柄
未设置应用程序。

因此,将 creationflags=0x08000000 添加到您的 Popen 调用中应该会有所帮助(不幸的是,我没有运行 Windows 的计算机来尝试此操作,因此您必须尝试一下)你自己)。

I imagine your observation is limited to Windows, since that, I believe, is the only platform on which you'll get that "console flash" issue. If so, then the docs offer the following semi-helpful paragraph:

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)

Unfortunately the Python online docs do not reproduce the relevant portion of the Windows API docs, so you have to locate those elsewhere, e.g. starting here on MSDN which leads you here for the creationflags, and specifically to

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.

So, adding creationflags=0x08000000 to your Popen call should help (unfortunately I have no Windows-running machine on which to try this out, so you'll have to try it yourself).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文