循环可执行文件以获取 Python 脚本的结果

发布于 2024-08-26 03:12:38 字数 560 浏览 5 评论 0原文

在我的 python 脚本中,我需要在 for 循环中调用可执行文件,并等待该可执行文件将结果写入“output.xml”。

我如何设法使用 wait() &我如何知道我的一个可执行文件何时完成生成结果以获得结果?如何关闭该进程并打开一个新进程以再次调用可执行文件并等待新结果?

    import subprocess
    args = ("bin/bar")
    popen = subprocess.Popen(args)

我需要等待“bin/bar”的输出来生成“output.xml”,然后从那里读取它的内容。

    for index, result in enumerate(results):
        myModule.callSubProcess(index)
        #this is where the problem is.
        fileOutput = open("output.xml")
        parseAndStoreInSQLiteFileOutput(index, file)

In my python script, I need to call within a for loop an executable, and waiting for that executable to write the result on the "output.xml".

How do I manage to use wait() & how do I know when one of my executable is finished generating the result to get the result? How do I close that process and open a new one to call again the executable and wait for the new result?

    import subprocess
    args = ("bin/bar")
    popen = subprocess.Popen(args)

I need to wait for the output from "bin/bar" to generate the "output.xml" and from there, read it's content.

    for index, result in enumerate(results):
        myModule.callSubProcess(index)
        #this is where the problem is.
        fileOutput = open("output.xml")
        parseAndStoreInSQLiteFileOutput(index, file)

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

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

发布评论

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

评论(2

生生漫 2024-09-02 03:12:38

Popen.wait()将使脚本等待,直到进程结束。之后无需终止该进程,因为它已经退出了。

Popen.wait() will make the script wait until the process ends. There's no need to kill the process afterwards, since it will have already exited.

写下不归期 2024-09-02 03:12:38

我认为最简单的方法是使用 call

import subprocess
retcode = subprocess.call('command', shell=True)

它等待进程终止并将返回代码分配给变量。
有关更详细的说明,请参阅 python 中的 17.1.subprocess - 便利函数文档。希望有帮助。

I think the easiest way to do is using call:

import subprocess
retcode = subprocess.call('command', shell=True)

It waits for the process to terminate and assign the return code to the variable.
For more detailed description see 17.1.subprocess - convenience functions in the python documentation. Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文