在不打开 Windows 命令提示符的情况下从 Python 执行子进程

编程入门 行业动态 更新时间:2024-10-21 19:47:50
本文介绍了在不打开 Windows 命令提示符的情况下从 Python 执行子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能的重复:在没有控制台的情况下使用 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"], shell=True)

更多推荐

在不打开 Windows 命令提示符的情况下从 Python 执行子进程

本文发布于:2023-11-08 20:35:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1570390.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令提示符   情况下   进程   Windows   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!