检索隐藏控制台的命令输出? [英] Retrieving the command output of a hidden console?

查看:348
本文介绍了检索隐藏控制台的命令输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从python环境运行一个DOS命令,通过stdout检索其命令输出。

I run a DOS command from a python environment which retrieves its command output via stdout.

所有工作正常,除了Windows控制台每次脚本运行时弹出,因此我需要一种隐藏Windows控制台的方法。

All is working fine excepted that the windows console pops out everytime the script runs, so I need a way to hide the windows console.

这里是预期的过程:

"Console1" runs "hidden Console2" and retrieves its output into "Console1"


$ b b

我在执行DOS命令和批处理脚本时隐藏控制台的软件 HiddenStart chp VBScript中的另一个解决方案也可以通过以下代码实现此结果:

I read some recommendations with softwares like "HiddenStart" or "chp" which hide the console when executing DOS commands and batch scripts. Another solution in VBScript can also achieve this result with the following code:

Set oShell = CreateObject ("Wscript.Shell")
Dim strArgs
strArgs = "cmd /c COMMAND GOES HERE"
oShell.Run strArgs, 0, false

但是,不幸的是,HiddenStart 和 chp 也不允许VBScript脚本检索执行命令的命令输出。

But unfortunately, neither "HiddenStart" nor "chp" nor the VBScript script allow to retrieve the command output of the executed commands. "Chp" outputs a stdout but only for the exit process code.

有些人可能会说,可以将结果记录到磁盘上的文件中,然后检索文件的内容,但这不是需要的,并导致一个更复杂的过程。

Some might say that one could log the result to a file on disk, and then retrieve the content of the file, but it is not what needed and results in a more complicated process.

所以,我想知道它是否可以从隐藏的控制台检索命令输出?

So, I'm wondering if it is at all possible to retrieve the command output from a hidden console, is it?

解决方案:

如何避免使用.pyw的控制台窗口包含os.system调用的文件?

POST的作者注:
这是我第一次来这里,这个问题不是重复,而是一个变体。证明这是我做了一个搜索之前,很难找到解决方案之前,我实际写的问题。因此,主持人需要比这更微妙一点。这是一个链接的,相关的问题或变体,但不是重复,我想。感谢。

NOTE OF THE AUTHOR OF THE POST: Hi, this is my first time here, and this question is not a duplicate but a variant. Proof to that is that i made a search before and it was difficult to find the solution before I actually wrote the question. So, moderators need to be a bit more subtle than that. This is a linked, related question or a variant, but not a duplicate, I think. Thanks.

推荐答案

以窗口为中心的解决方案可以基于此VBScript

A windows-centric solution could be based on a 'port' from this VBScript:

mother.py:

mother.py:

import win32com.client
oWS = win32com.client.Dispatch("WScript.Shell")
print("A", "mother starts child")
oEx = oWS.Exec("cscript ..\\vbs\\child.vbs")
while not oEx.Stdout.AtEndOfStream:
   print(oEx.Stdout.ReadLine())
print("B", "mother done")

输出:

A mother starts child
1 child
2 child
3 child
4 child
5 child
B mother done

(ActiveState Python 3.2.2;从 print 中删除​​(),得到一个2.x版本(使用ActiveState Python 2.5.2测试);使用 dir ,prepend%comspec%/ c)

(ActiveState Python 3.2.2; removing the () from the prints gives a 2.x version (tested with ActiveState Python 2.5.2); to use intrinsics like dir, prepend "%comspec% /c ")

这篇关于检索隐藏控制台的命令输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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