python 在不打开控制台的情况下调用外部程序(dos 提示) [英] python calling external programs without opening console (dos prompt)

查看:31
本文介绍了python 在不打开控制台的情况下调用外部程序(dos 提示)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
在没有控制台的情况下使用 Popen 在 pythonw 中运行进程
如何消除 Windows 控制台来自 Python (2.7) 中生成的进程?

我使用 Python 2.7 并从 IDLE 中运行 Python 脚本.

I am using Python 2.7 and running the python scripts from within IDLE.

我正在执行的命令是执行快速任务的简单 exe.我遇到的问题是每次从 Python 中调用外部命令时,都会创建一个 控制台,它会在我的屏幕上闪烁并获得焦点,从而阻止我在执行各种脚本时使用我的 PC.

The commands I am executing are simple exe's that perform quick tasks. The issue I am having is every time the external commands are called from within Python a console is created and it flashes on my screen and takes focus, thus preventing me from using my PC while executing various scripts.

我如何在 Python 中调用它们的示例如下:

Examples of how I am calling them from within Python are as follows:

result = call(["Commands\Set.exe", str(i), ARG2])
check_output(["Commands\Read.exe", ARG2])

在寻找解决方案时我遇到了添加以下内容

Searching for a solution I came across adding the following

shell=True

执行以下命令

check_output(["Commands\Read.exe", ARG2], shell=True)

但是每次调用外部命令时我仍然会出现控制台

However I still get the console appear every time an external command is called

推荐答案

这里可能有两个问题.首先,如果您的 python 脚本具有 .pyw 扩展名,那么它们将与不使用控制台 * 的 pythonw 相关联.但是,您有 shell=True,它会生成一个控制台*.您需要运行程序并隐藏控制台:

There might be two issues here. First off, if your python scripts have the .pyw extension then they will be associated with pythonw which does not use a console*. However, you have shell=True, which generates a console*. You need to run the program and hide the console:

import subprocess
proc = subprocess.Popen('hello.py',  creationflags=subprocess.SW_HIDE, shell=True)
proc.wait()

*迂腐地,它不是一个 dos 提示,它是一个控制台窗口.DOS - 磁盘操作系统 - 是 IBM 大型机操作系统.MS-DOS 或 PC-DOS 命令行功能由 cmd.exe(Windows 外壳程序)镜像(具有许多额外功能),cmd.exe 是一个控制台程序,因此使用控制台窗口.这是您需要隐藏的控制台窗口.

*Pedantically, it's not a dos prompt, it is a console window. DOS - Disk Operating System - was an IBM mainframe OS. MS-DOS or PC-DOS command-line features were mirrored (with a lot of extra features) by cmd.exe (a Windows shell), which is a console program and so uses a console window. It's that console window you need to hide.

这篇关于python 在不打开控制台的情况下调用外部程序(dos 提示)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆