从 Python 执行子进程而不打开 Windows 命令提示符

Executing subprocess from Python without opening Windows Command Prompt(从 Python 执行子进程而不打开 Windows 命令提示符)
本文介绍了从 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)

这篇关于从 Python 执行子进程而不打开 Windows 命令提示符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)