实时提取.exe的输出并存储

发布于 2025-01-28 21:53:50 字数 954 浏览 4 评论 0原文

我有一个.exe程序,可以生成实时数据。我想实时运行程序时提取输出,但是这是我第一次尝试此操作,因此我需要帮助解决此问题。

我已经以以下内容打开它:

cmd = r'/Applications/StockSpy Realtime Stocks Quote.app/Contents/MacOS/StockSpy Realtime Stocks Quote'

import subprocess

with open('output.txt', 'wb') as f:
    subprocess.check_call(cmd, stdout=f)

# to read line by line
with open('output.txt') as f:
    for line in f:
        print(line)
# output = qx(cmd)

旨在存储输出。但是,它不能保存任何输出,我得到了一个空白的文本文件。

我设法通过遵循以下代码来保存输出:

from subprocess import STDOUT, check_call as x

with open(os.devnull, 'rb') as DEVNULL, open('output.txt', 'wb') as f:
    x(cmd, stdin=DEVNULL, stdout=f, stderr=STDOUT)

frof 我如何使用子进程和popen从.exe中获取所有输出?

I have a .exe programme that produces real-time data. I want to extract the output when running the programme in real-time, however It's my first time trying this out, and so I wanted help in approaching this.

I have opened it with the following:

cmd = r'/Applications/StockSpy Realtime Stocks Quote.app/Contents/MacOS/StockSpy Realtime Stocks Quote'

import subprocess

with open('output.txt', 'wb') as f:
    subprocess.check_call(cmd, stdout=f)

# to read line by line
with open('output.txt') as f:
    for line in f:
        print(line)
# output = qx(cmd)

with the aim to store the output. However, it does not save any of the output, I get a blank textfile.

I managed to save the output by following this code:

from subprocess import STDOUT, check_call as x

with open(os.devnull, 'rb') as DEVNULL, open('output.txt', 'wb') as f:
    x(cmd, stdin=DEVNULL, stdout=f, stderr=STDOUT)

from How do I get all of the output from my .exe using subprocess and Popen?

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

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

发布评论

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

评论(1

小矜持 2025-02-04 21:53:50

使用类似的东西,Python可以实现您要做的事情:

import subprocess
with subprocess.Popen(['/path/to/executable'], stdout=subprocess.PIPE) as proc:
    data = proc.stdout.read()   # the data variable will contain the 
                                # what would usually be the output 
    """Do something with data..."""

What you are trying to do can be achieved with python using something like this:

import subprocess
with subprocess.Popen(['/path/to/executable'], stdout=subprocess.PIPE) as proc:
    data = proc.stdout.read()   # the data variable will contain the 
                                # what would usually be the output 
    """Do something with data..."""
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文