循环可执行文件以获取 Python 脚本的结果
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.我认为最简单的方法是使用
call
:它等待进程终止并将返回代码分配给变量。
有关更详细的说明,请参阅 python 中的 17.1.subprocess - 便利函数文档。希望有帮助。
I think the easiest way to do is using
call
: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.